Skip to content

Commit

Permalink
Added configuration module and tied logging parameters into it
Browse files Browse the repository at this point in the history
  • Loading branch information
jwtd committed May 5, 2014
1 parent b62a2e6 commit bfde716
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 142 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ build/
# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
# .ruby-version
# .ruby-gemset
.ruby-version
.ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# Ignore log files
*.log
*.log.age
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in ekm-meter.gemspec
gemspec


16 changes: 16 additions & 0 deletions examples/read_ekm_meter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

require 'ekm-omnimeter'

# Block style configuration
EkmOmnimeter.configure do |c|

# Logging Configuration
c.log_level = 'debug' # :off, :all, :debug, :info, :warn, :error, :fatal
c.trace_exceptions = true # Default is true
c.log_to_stdout = false # Default is true
c.stdout_colors = :for_dark_background
c.log_file = 'ekm.log' # Default is nil
c.log_file_layout = '[%d] %-5l -- %c -- %m\n' # :basic, :json, :yaml, or a pattern such as '[%d] %-5l: %m\n'
c.rolling_log_file_age = :daily # Default is false
c.rolling_log_limit = 11 # Default is false, but any positive integer can be passed
c.growl_on_error = false # Default is false

end

# Connect to a meter
m = EkmOmnimeter::Meter.new(
:power_configuration => :single_phase_3wire,
Expand Down
1 change: 1 addition & 0 deletions lib/ekm-omnimeter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class EkmOmnimeterError < ::Exception; end

require "ekm-omnimeter/version"
require "ekm-omnimeter/logger"
require "ekm-omnimeter/configuration"
require "ekm-omnimeter/crc16"
require "ekm-omnimeter/meter"
36 changes: 34 additions & 2 deletions lib/ekm-omnimeter/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
module EkmOmnimeter

# Module level access to configuration
class << self
attr_writer :configuration
end

# Lazy initialization of default config
def self.configuration
@configuration ||= Configuration.new
end

# Reset configuration
def self.reset
@configuration = Configuration.new
end

# Allow block style configuration
def self.configure
yield(configuration)
end

# Define the configuration options
class Configuration
attr_accessor :sockets

attr_accessor :log_level # :off, :all, :debug, :info, :warn, :error, :fatal
attr_accessor :trace_exceptions # Default is true
attr_accessor :log_to_stdout # Default is true
attr_accessor :stdout_colors # Default is :for_dark_backgroundm, :for_light_background, or custom by passing a hash that conforms to https://github.com/TwP/logging/blob/master/examples/colorization.rb
attr_accessor :log_file # Default is nil
attr_accessor :log_file_layout # :basic, :json, :yaml, or a pattern such as '[%d] %-5l: %m\n'
attr_accessor :rolling_log_file_age # Default is false, options are false or 'daily', 'weekly', 'monthly' or an integer
attr_accessor :rolling_log_limit # Default is false, but any positive integer can be passed
attr_accessor :growl_on_error # Default is false

# Specify the configuration defaults and support configuration via hash .onfiguration.new(config_hash)
def initialize(options={})

log_level = options[:log_level] || :off
trace_exceptions = options[:trace_exceptions] || true
log_to_stdout = options[:log_to_stdout] || true
stdout_colors = options[:stdout_colors] || :for_dark_background
log_file = options[:log_file] || nil
log_file_layout = options[:log_file_layout] || :basic
rolling_log_file_age = options[:rolling_log_file_age] || false
rolling_log_limit = options[:rolling_log_limit] || false
growl_on_error = options[:growl_on_error] || false

end

end

end
end


231 changes: 96 additions & 135 deletions lib/ekm-omnimeter/logger.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
require 'logging'

# Logging.init is required to avoid
# unknown level was given 'info' (ArgumentError)
# or
# uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
#Logging.init :debug, :info, :warn, :error, :fatal
#
#layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"
#
## Default logfile, history kept for 10 days
#default_appender = Logging::Appenders::RollingFile.new 'default', \
# :filename => 'log/default.log', :age => 'daily', :keep => 10, :safe => true, :layout => layout
#
## Audit logfile, history kept forever
#audit_appender = Logging::Appenders::RollingFile.new 'audit', \
# :filename => 'log/audit.log', :age => 'daily', :safe => true, :layout => layout
#
## Production logfile, history kept forever
#prod_appender = Logging::Appenders::RollingFile.new 'prod', \
# :filename => 'log/production.log', :age => 'daily', :safe => true, :layout => layout
#
#DEFAULT_LOGGER = returning Logging::Logger['server'] do |l|
# l.add_appenders default_appender
#end

module EkmOmnimeter

@@global_logger = nil
Expand All @@ -13,33 +38,52 @@ def self.global_logger_configured?
def self.configure_global_logger(options={})

# Collect and validate log level
log_level = options[:log_level] || :debug
raise "Can not initialize global logger, because #{log_level.inspect} is an unrecognized log level." unless [:off, :all, :debug, :info, :warn, :error, :fatal].include?(log_level)
Logging.logger.root.level = log_level
log_level = options[:log_level] || configuration.log_level
raise "Can not initialize global logger, because #{log_level.inspect} is an unrecognized log level." unless [:off, :all, :debug, :info, :warn, :error, :fatal].include?(log_level.to_sym)
Logging.logger.root.level = log_level.to_sym

# When set to true backtraces will be written to the logs
trace_exceptions = options[:trace_exceptions] || true
trace_exceptions = options[:trace_exceptions] || configuration.trace_exceptions
Logging.logger.root.trace = trace_exceptions

# Setup colorized output for stdout (this scheme was setup for a terminal with a black background)
# :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white
# :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
# :blink, :bold, :underline, :underscore
stdout_colors = options[:stdout_colors] || {:levels => {
:debug => :white,
:info => [:white, :on_blue, :bold],
:warn => [:black, :on_yellow, :bold] ,
:error => [:white, :on_red, :bold],
:fatal => [:white, :on_red, :bold, :blink]
},
:date => :yellow,
:logger => :cyan,
:message => :white}
Logging.color_scheme('stdout_colors', stdout_colors)

# Always log info to stdout
log_to_stdout = options[:log_to_stdout] || true
# Check to see if we should log to stdout
log_to_stdout = options[:log_to_stdout] || configuration.log_to_stdout
if log_to_stdout

# Setup colorized output for stdout (this scheme was setup for a terminal with a black background)
# :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white
# :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
# :blink, :bold, :underline, :underscore
stdout_colors = options[:stdout_colors] || configuration.stdout_colors
if stdout_colors == :for_dark_background
Logging.color_scheme('stdout_colors',
{:levels => {
:debug => :white,
:info => [:white, :on_blue, :bold],
:warn => [:black, :on_yellow, :bold] ,
:error => [:white, :on_red, :bold],
:fatal => [:white, :on_red, :bold, :blink]
},
:date => :yellow,
:logger => :cyan,
:message => :white})
elsif stdout_colors == :for_light_background
Logging.color_scheme('stdout_colors',
{:levels => {
:debug => :black,
:info => [:black, :on_blue, :bold],
:warn => [:black, :on_yellow, :bold] ,
:error => [:white, :on_red, :bold],
:fatal => [:white, :on_red, :bold, :blink]
},
:date => :blue,
:logger => :magenta,
:message => :black})
elsif stdout_colors.is_a?(Hash)
Logging.color_scheme('stdout_colors', stdout_colors)
end

# Add the stdout appender
Logging.logger.root.add_appenders Logging.appenders.stdout(
'stdout',
:layout => Logging.layouts.pattern(
Expand All @@ -49,35 +93,43 @@ def self.configure_global_logger(options={})
)
end

if options[:log_file]

# Setup file logger
log_file = options[:log_file] || configuration.log_file
if log_file

# Make sure log directory exists
log_file = File.expand_path(options[:log_file])
log_file = File.expand_path(log_file)
log_dir = File.dirname(log_file)
raise "The log file can not be created, because its directory does not exist #{log_dir}" if Dir.exists?(log_dir)

raise "The log file can not be created, because its directory does not exist #{log_dir}" unless Dir.exists?(log_dir)

# Determine layout. The available layouts are :basic, :json, :yaml, or a pattern such as '[%d] %-5l: %m\n'
layout = options[:log_file_layout] || :basic
if options[:log_file_layout]
if layout == :basic
use_layout = Logging.layouts.basic
elsif layout == :json
use_layout = Logging.layouts.json
elsif layout == :yaml
use_layout = Logging.layouts.yaml
else
use_layout = Logging.layouts.pattern(:pattern => layout) # '[%d] %-5l: %m\n'
end
layout = options[:log_file_layout] || configuration.log_file_layout
if layout == :basic
use_layout = Logging::Layouts::Basic.new()
elsif layout == :json
use_layout = Logging::Layouts::JSON.new
elsif layout == :yaml
use_layout = Logging::Layouts::YAML.new
else
use_layout = Logging.layouts.pattern(:pattern => layout) # '[%d] %-5l -- %c -- %m\n'
end

# Make sure we're capturing milliseconds in date
use_layout.date_pattern = '%Y-%m-%d %H:%M:%S:%L'

# Determine if this should be a single or rolling log file
rolling = options[:rolling_log_file_age] || false
rolling = options[:rolling_log_file_age] || configuration.rolling_log_file_age

# Build the file appender
if rolling
rolling_limit = options[:rolling_log_limit] || configuration.rolling_log_limit
Logging.logger.root.add_appenders Logging.appenders.rolling_file(
'development.log',
:age => rolling,
log_file,
:age => rolling,
:keep => rolling_limit,
:safe => true,
:layout => use_layout
)
else
Expand All @@ -86,12 +138,12 @@ def self.configure_global_logger(options={})
end

# Growl on error
growl_on_error = options[:growl_on_error] || false
growl_on_error = options[:growl_on_error] || configuration.growl_on_error
if options[:growl_on_error]
Logging.logger.root.add_appenders Logging.appenders.growl(
'growl',
:level => :error,
:layout => Logging.layouts.pattern(:pattern => '[%d] %-5l: %m\n')
:layout => use_layout #Logging.layouts.pattern(:pattern => '[%d] %-5l: %m\n')
)
end

Expand All @@ -113,101 +165,10 @@ def self.logger

def self.configure_logger_for(classname)
EkmOmnimeter.configure_global_logger() unless EkmOmnimeter.global_logger_configured?
l = Logging.logger[classname]
Logging.logger[classname]
end

#def self.configure_logger_for(classname, options={})
#
# # Create the logger
# l = Logging.logger[classname]
#
# # Collect and validate log level
# log_level = options[:log_level] || :debug
# raise "Can not initalize global logger, because #{log_level.inspect} is an unrecognized log level." unless [:off, :all, :debug, :info, :warn, :error, :fatal].include?(log_level)
# Logging.logger.root.level = log_level
#
# # When set to true backtraces will be written to the logs
# trace_exceptions = options[:trace_exceptions] || true
# l.trace = trace_exceptions
#
# # Setup colorized output for stdout (this scheme was setup for a terminal with a black background)
# # :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white
# # :on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white
# # :blink, :bold, :underline, :underscore
# stdout_colors = options[:stdout_colors] || {:levels => {
# :debug => :white,
# :info => [:white, :on_blue, :bold],
# :warn => [:black, :on_yellow, :bold] ,
# :error => [:white, :on_red, :bold],
# :fatal => [:white, :on_red, :bold, :blink]
# },
# :date => :yellow,
# :logger => :cyan,
# :message => :white}
# Logging.color_scheme('stdout_colors', stdout_colors)
#
# # Always log info to stdout
# log_to_stdout = options[:log_to_stdout] || true
# if log_to_stdout
# l.add_appenders Logging.appenders.stdout(
# 'stdout',
# :layout => Logging.layouts.pattern(
# #:pattern => '[%d] %-5l %c: %m\n',
# :color_scheme => 'stdout_colors'
# )
# )
# end
#
# if options[:log_file]
#
# # Make sure log directory exists
# log_file = File.expand_path(options[:log_file])
# log_dir = File.dirname(log_file)
# raise "The log file can not be created, because its directory does not exist #{log_dir}" if Dir.exists?(log_dir)
#
# # Determine layout. The available layouts are :basic, :json, :yaml, or a pattern such as '[%d] %-5l: %m\n'
# layout = options[:log_file_layout] || :basic
# if options[:log_file_layout]
# if layout == :basic
# use_layout = Logging.layouts.basic
# elsif layout == :json
# use_layout = Logging.layouts.json
# elsif layout == :yaml
# use_layout = Logging.layouts.yaml
# else
# use_layout = Logging.layouts.pattern(:pattern => layout) # '[%d] %-5l: %m\n'
# end
# end
#
# # Determine if this should be a single or rolling log file
# rolling = options[:rolling_log_file_age] || false
#
# # Build the file appender
# if rolling
# l.add_appenders Logging.appenders.rolling_file(
# 'development.log',
# :age => rolling,
# :layout => use_layout
# )
# else
# # Non-rolling log file
# l.add_appenders Logging.appenders.file(log_file, :layout => use_layout)
# end
#
# # Growl on error
# growl_on_error = options[:growl_on_error] || false
# if options[:growl_on_error]
# l.add_appenders Logging.appenders.growl(
# 'growl',
# :level => :error,
# :layout => Logging.layouts.pattern(:pattern => '[%d] %-5l: %m\n')
# )
# end
#
# end
#
# l
#
end

end
end

Loading

0 comments on commit bfde716

Please sign in to comment.