lib/locd/logging.rb in locd-0.1.3 vs lib/locd/logging.rb in locd-0.1.4

- old
+ new

@@ -270,39 +270,78 @@ def self.setup? !!@setup end - def self.get_env_level - if ENV['LOCD_TRACE'].truthy? + def self.get_config_level + if Locd.config[:trace].truthy? return :trace - elsif ENV['LOCD_DEBUG'].truthy? + elsif Locd.config[:debug].truthy? return :debug - elsif ENV['LOCD_LOG_LEVEL'] - return ENV['LOCD_LOG_LEVEL'].to_sym + elsif level = Locd.config[:log, :level] + return level.to_sym end nil end + def self.set_config_levels + if levels = Locd.config[:log, :levels] + levels.each do |name, level| + const = begin + NRSER.to_const name + rescue + next + end + + level = level.to_sym + + self.logger.debug "Setting logger level", + logger: const.logger, + level: level + + const.logger.level = level + + self.logger.debug "Logger level set", + logger: const.logger + end + end + end + + # Setup logging. # - # @param [type] arg_name - # @todo Add name param description. + # @param [Symbol?] level: + # Optional log level. If provided, it takes precedence. Otherwise, we will + # look for log levels in the ENV vars. If nothing is found, no default + # log level will be set - it will remain whatever default it already is + # (usually `:info` unless something else has changed it). # - # @return [return_type] - # @todo Document return value. + # @param [Boolean] sync: + # When `true`, we will hack out Semantic Logger's async (threaded) + # processor and install a synchronous one, which is what you want when + # running from the CLI so that output is in order with execution (we don't + # care about the performance implications there). # + # @param [IO | Hash | String] dest: + # Set the appender, see {.appender=}. + # + # @return [true] + # If logging was setup. + # + # @return [false] + # If logging has already been setup (warning will be logged as well). + # def self.setup level: nil, sync: false, dest: nil if setup? logger.warn "Logging is already setup!" return false end SemanticLogger.application = 'locd' - level = get_env_level if level.nil? + level = get_config_level if level.nil? self.level = level if level self.appender = dest if dest if sync # Hack up SemanticLogger to do sync logging in the main thread