lib/site_hook/logger.rb in site_hook-0.8.2 vs lib/site_hook/logger.rb in site_hook-0.9.3
- old
+ new
@@ -1,194 +1,115 @@
-require 'paint'
-require 'logging'
-require 'pathname'
-require 'active_support/core_ext/string'
-
-Logging.init %w(NONE DEBUG INFO WARN ERROR FATAL)
-Logging.color_scheme(
- 'bright',
- :levels => {
- :debug => [:yellow, :on_white],
- :info => :blue,
- :warn => :yellow,
- :error => :red,
- :fatal => [:red, :on_white],
- },
- :date => :white,
- :logger => :cyan,
- :message => :green,
- )
-PATTERN = '[%d] %-5l %c: %m\n'
-DATE_PATTERN = '%Y-%m-%d %H:%M:%S'
-layout = Logging.layouts.pattern \
- :pattern => PATTERN,
- :date_pattern => DATE_PATTERN,
- :color_scheme => 'bright'
-
-Logging.appenders.stdout \
- :layout => layout
-
+require 'logger'
+require 'recursive_open_struct'
+require 'site_hook/loggers'
+require 'site_hook/paths'
+require 'yaml'
+require 'site_hook/string_ext'
+#require 'site_hook/configs'
module SiteHook
- def mklogdir
- path = Pathname(Dir.home).join('.jph', 'logs')
- if path.exist?
- # Path exists, don't do anything
- else
- FileUtils.mkpath(path.to_s)
+ class Log
+ def self.defaults
+ RecursiveOpenStruct.new(
+ {
+ Hook: {
+ level: 'info'
+ },
+ App: {
+ level: 'info'
+ },
+ Build: {
+ level: 'info'
+ },
+ Git: {
+ level: 'info'
+ },
+ Access: {
+ level: nil
+ },
+ Fake: {
+ level: nil
+ }
+ })
end
- end
- def safe_log_name(klass)
- klass.class.name.split('::').last.underscore
- end
-
- module_function :mklogdir, :safe_log_name
-
- class LogLogger
- attr :log
- attr :log_level
-
- def initialize(log_level = 'info') # only change this to debug when testing
- @log = Logging.logger[SiteHook.safe_log_name(self)]
- @log_level = log_level
-
- flayout = Logging.appenders.rolling_file \
- Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
- :age => 'daily',
- :pattern => PATTERN
- @log.add_appenders 'stdout', flayout
- @log.level = log_level
- end
- end
-
- mklogdir
- LL = LogLogger.new.log
- LL.debug "#{SiteHook.safe_log_name(LL.class)} initialized."
-
- class HookLogger
-
- # Log App Actions
- class AppLog
- attr :log
- attr :log_level
-
- def initialize(log_level = nil)
- LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
- @log = Logging.logger[SiteHook.safe_log_name(self)]
- @log_level = log_level
-
- flayout = Logging.appenders.rolling_file \
- Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
- :age => 'daily',
- :pattern => PATTERN
- @log.add_appenders 'stdout', flayout
- @log.level = log_level
- LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
+ def self.validate(config)
+ invalid_types = []
+ valid_config_log_types = [
+ 'hook',
+ 'git',
+ 'app',
+ 'build'
+ ]
+ invalid_config_log_types = [
+ 'access',
+ 'fake'
+ ]
+ config = config['log_levels']
+ is_config_valid = config.all? do |x|
+ if valid_config_log_types.include? x
+ true
+ else
+ if invalid_config_log_types.include? x
+ invalid_types << x
+ false
+ end
+ end
end
+ unless is_config_valid
+ raise ArgumentError "invalid log type(s) in config, [#{invalid_types.join(', ')}]"
+ end
end
- # Log Hook Actions
- class HookLog
- attr :log
- attr :log_level
-
- def initialize(log_level = nil)
- LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
- @log = Logging.logger[SiteHook.safe_log_name(self)]
- @log_level = log_level
- flayout = Logging.appenders.rolling_file \
- Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
- :age => 'daily',
- :pattern => PATTERN
- @log.add_appenders 'stdout', flayout
- @log.level = @log_level
- LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
+ def inspect
+ meths = %i[hook build git app fake access]
+ sections = {}
+ meths.each do |m|
+ sections[m] = self.class.send(m).inspect
end
+ secs = []
+ sections.each { |name, instance| secs << "#{name}=#{instance}" }
+ "#<SiteHook::Log #{secs.join(' ')}>"
end
- # Log Build Actions
- class BuildLog
- attr :log
+ def initialize(input, output, errput)
- def initialize(log_level = nil)
- LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
- @log = Logging.logger[SiteHook.safe_log_name(self)]
- @log_level = log_level
+ begin
+ @@config = SiteHook::Config.log_levels
+ rescue Errno::ENOENT
+ raise NoConfigError path
+ rescue NoMethodError
- flayout = Logging.appenders.rolling_file \
- Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
- :age => 'daily',
- :pattern => PATTERN
- @log.add_appenders 'stdout', flayout
- @log.level = log_level
-
- LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
end
+
end
- # Log Git Actions
- class GitLog
- attr :log
+ # @return [Access]
+ def self.access
+ Loggers::Access.new(base: 'SiteHook::Log::Access')
+ end
- def initialize(log_level = nil)
- LL.debug "Initializing #{SiteHook.safe_log_name(self)}"
- @log = Logging.logger[SiteHook.safe_log_name(self)]
- @log_level = log_level
+ # @return [Loggers::Fake]
+ def self.fake
+ Loggers::Fake.new
+ end
- flayout = Logging.appenders.rolling_file \
- Pathname(Dir.home).join('.jph', 'logs', "#{SiteHook.safe_log_name(self)}-#{@log_level}.log").to_s,
- :age => 'daily',
- :pattern => PATTERN
- @log.add_appenders 'stdout', flayout
- @log.level = log_level
- LL.debug "Initialized #{SiteHook.safe_log_name(self)}"
- end
+ # @return [Loggers::Hook]
+ def self.hook
+ Loggers::Hook.new(level: @@config.hook, base: 'SiteHook::Log::Hook')
end
- # Fake Logger for GitLog to preprocess output
- class FakeLog < StringIO
- attr :info_output, :debug_output
- def initialize
- @info_output = []
- @debug_output = []
- end
- # @param [Any] message message to log
- def info(message)
- case
- when message =~ /git .* pull/
- @info_output << "Starting Git"
- @debug_output << message
- else
- @debug_output << message
- end
- end
- # @param [Any] message message to log
- def debug(message)
- case
- when message =~ /\n/
- msgs = message.lines
- msgs.each do |msg|
- msg.squish!
- case
- when msg =~ /From (.*?):(.*?)\/(.*)(\.git)?/
- @info_output << "Pulling via #{$2}/#{$3} on #{$1}."
- when msg =~ /\* branch (.*?) -> .*/
- @info_output << "Using #{$1} branch"
- else
- @debug_output << msg
- end
- end
- else
- @debug_output << message
- end
- end
+ # @return [Loggers::Git]
+ def self.git
+ Loggers::Git.new(level: @@config.git, base: 'SiteHook::Log::Git')
+ end
- # @return [Hash] Hash of log entries
- def entries
- {
- info: @info_output,
- debug: @debug_output
- }
- end
+ # @return [Loggers::Build]
+ def self.build
+ Loggers::Build.new(level: @@config.build, base: 'SiteHook::Log::Build')
end
+
+ # @return [Loggers::App]
+ def self.app
+ Loggers::App.new(level: @@config.app, base: 'SiteHook::Log::App')
+ end
end
-end
+end
\ No newline at end of file