logsly 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 2dfa76f82b146db11ebe7e52e8ac9bb51f0fc9c8
4
- data.tar.gz: 9dbc80bdfe08dce4f6acacf50f29aca0b90965ea
5
- SHA512:
6
- metadata.gz: 1629d93cc3f4b0f8979ac796ef219ccf05aecc85750829029f20c1f5c48d8dd05117b65e27112ca7509530152dbec2a01ad04b068b127e7a41ce513667bb16c8
7
- data.tar.gz: 0abc0262c174e4e12a31c8a81f659888379ce5ec9538bbbbffab0c2d4efd1b0b414c3b27befa7a15a7be6c9dbdfbb02075bff6d4cdeac44858511c22272d2518
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 873d513fcda4a6f898be9fbea3fe557f36e25174
4
+ data.tar.gz: ed137cb5edb8b14102f43cdbc60bcfef30968007
5
+ SHA512:
6
+ metadata.gz: 30f4de19dc9c57407713ab504f727fe309e83632e52f46b548aa1b39bfcf4b5cff8def086a8e7599d5818b04f4377756022c8a9641df6b9ae4f86a3675c8624a
7
+ data.tar.gz: fb1fae8744f5e4652bf8252a08e50fe15f403e5e437686c2d5e836b06e67d03cff52ccd366cb59491a46c758b1c94651a8556042d3159ed19916b4ca627ff05d
data/Gemfile CHANGED
@@ -2,5 +2,4 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake', "~> 10.4.0"
6
5
  gem 'pry', "~> 0.9.0"
File without changes
data/lib/logsly/colors.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'ostruct'
2
- require 'ns-options'
3
-
4
1
  # This class provides a DSL for setting color scheme values and lazy eval's
5
2
  # the DSL to generate a Logging color scheme object.
6
3
  # See https://github.com/TwP/logging/blob/master/lib/logging/color_scheme.rb
@@ -8,6 +5,10 @@ require 'ns-options'
8
5
 
9
6
  module Logsly
10
7
 
8
+ class NullColors
9
+ def to_scheme(*args); nil; end
10
+ end
11
+
11
12
  class Colors
12
13
 
13
14
  attr_reader :name, :build
@@ -24,51 +25,135 @@ module Logsly
24
25
 
25
26
  end
26
27
 
27
- class NullColors < OpenStruct
28
- def to_scheme(*args); nil; end
29
- end
30
-
31
28
  class ColorsData
32
- include NsOptions::Proxy
33
-
34
- # color for the level text only
35
- option :debug
36
- option :info
37
- option :warn
38
- option :error
39
- option :fatal
40
-
41
- # color for the entire log message based on the value of the log level
42
- option :debug_line
43
- option :info_line
44
- option :warn_line
45
- option :error_line
46
- option :fatal_line
47
-
48
- option :logger # [%c] name of the logger that generate the log event
49
- option :date # [%d] datestamp
50
- option :message # [%m] the user supplied log message
51
- option :pid # [%p] PID of the current process
52
- option :time # [%r] the time in milliseconds since the program started
53
- option :thread # [%T] the name of the thread Thread.current[:name]
54
- option :thread_id # [%t] object_id of the thread
55
- option :file # [%F] filename where the logging request was issued
56
- option :line # [%L] line number where the logging request was issued
57
- option :method_name # [%M] method name where the logging request was issued
58
29
 
59
30
  def initialize(*args, &build)
60
31
  self.instance_exec(*args, &build)
61
32
 
62
- @properties = properties.map{|p| self.send(p)}
33
+ @properties = properties.map{ |p| self.send(p) }
63
34
  @method = self.method_name
64
- @level_settings = levels.map{|l| self.send(l)}
65
- @line_settings = levels.map{|l| self.send("#{l}_line")}
35
+ @level_settings = levels.map{ |l| self.send(l) }
36
+ @line_settings = levels.map{ |l| self.send("#{l}_line") }
66
37
 
67
38
  if has_level_settings? && has_line_settings?
68
39
  raise ArgumentError, "can't set line and level settings in the same scheme"
69
40
  end
70
41
  end
71
42
 
43
+ # color for the level text only
44
+
45
+ def debug(value = nil)
46
+ @debug = value if !value.nil?
47
+ @debug
48
+ end
49
+
50
+ def info(value = nil)
51
+ @info = value if !value.nil?
52
+ @info
53
+ end
54
+
55
+ def warn(value = nil)
56
+ @warn = value if !value.nil?
57
+ @warn
58
+ end
59
+
60
+ def error(value = nil)
61
+ @error = value if !value.nil?
62
+ @error
63
+ end
64
+
65
+ def fatal(value = nil)
66
+ @fatal = value if !value.nil?
67
+ @fatal
68
+ end
69
+
70
+ # color for the entire log message based on the value of the log level
71
+
72
+ def debug_line(value = nil)
73
+ @debug_line = value if !value.nil?
74
+ @debug_line
75
+ end
76
+
77
+ def info_line(value = nil)
78
+ @info_line = value if !value.nil?
79
+ @info_line
80
+ end
81
+
82
+ def warn_line(value = nil)
83
+ @warn_line = value if !value.nil?
84
+ @warn_line
85
+ end
86
+
87
+ def error_line(value = nil)
88
+ @error_line = value if !value.nil?
89
+ @error_line
90
+ end
91
+
92
+ def fatal_line(value = nil)
93
+ @fatal_line = value if !value.nil?
94
+ @fatal_line
95
+ end
96
+
97
+ # [%c] name of the logger that generate the log event
98
+ def logger(value = nil)
99
+ @logger = value if !value.nil?
100
+ @logger
101
+ end
102
+
103
+ # [%d] datestamp
104
+ def date(value = nil)
105
+ @date = value if !value.nil?
106
+ @date
107
+ end
108
+
109
+ # [%m] the user supplied log message
110
+ def message(value = nil)
111
+ @message = value if !value.nil?
112
+ @message
113
+ end
114
+
115
+ # [%p] PID of the current process
116
+ def pid(value = nil)
117
+ @pid = value if !value.nil?
118
+ @pid
119
+ end
120
+
121
+ # [%r] the time in milliseconds since the program started
122
+ def time(value = nil)
123
+ @time = value if !value.nil?
124
+ @time
125
+ end
126
+
127
+ # [%T] the name of the thread Thread.current[:name]
128
+ def thread(value = nil)
129
+ @thread = value if !value.nil?
130
+ @thread
131
+ end
132
+
133
+ # [%t] object_id of the thread
134
+ def thread_id(value = nil)
135
+ @thread_id = value if !value.nil?
136
+ @thread_id
137
+ end
138
+
139
+ # [%F] filename where the logging request was issued
140
+ def file(value = nil)
141
+ @file = value if !value.nil?
142
+ @file
143
+ end
144
+
145
+ # [%L] line number where the logging request was issued
146
+ def line(value = nil)
147
+ @line = value if !value.nil?
148
+ @line
149
+ end
150
+
151
+ # [%M] method name where the logging request was issued
152
+ def method_name(value = nil)
153
+ @method_name = value if !value.nil?
154
+ @method_name
155
+ end
156
+
72
157
  def to_scheme_opts
73
158
  Hash.new.tap do |opts|
74
159
  # set general properties
@@ -0,0 +1,151 @@
1
+ require 'logging'
2
+ require 'syslog'
3
+
4
+ module Logsly; end
5
+ module Logsly::Outputs
6
+
7
+ ## NULL
8
+
9
+ class Null
10
+ def to_appender(*args); nil; end
11
+ def to_layout(*args); nil; end
12
+ end
13
+
14
+ ## BASE
15
+
16
+ class Base
17
+
18
+ attr_reader :build
19
+
20
+ def initialize(&build)
21
+ @build = build || Proc.new{}
22
+ end
23
+
24
+ def to_appender(*args)
25
+ self.instance_exec(*args, &@build)
26
+ self.colors_obj.run_build(*args)
27
+ self
28
+ end
29
+
30
+ def to_layout(data)
31
+ Logging.layouts.pattern(data.to_pattern_opts)
32
+ end
33
+
34
+ def to_appender(*args)
35
+ raise NotImplementedError
36
+ end
37
+
38
+ end
39
+
40
+ class BaseData
41
+
42
+ def initialize(*args, &build)
43
+ @pattern = '%m\n'
44
+ @colors = nil
45
+
46
+ @args = args
47
+ self.instance_exec(*@args, &(build || Proc.new{}))
48
+ end
49
+
50
+ def pattern(value = nil)
51
+ @pattern = value if !value.nil?
52
+ @pattern
53
+ end
54
+
55
+ def colors(value = nil)
56
+ @colors = value if !value.nil?
57
+ @colors
58
+ end
59
+
60
+ def to_pattern_opts
61
+ Hash.new.tap do |opts|
62
+ opts[:pattern] = self.pattern if self.pattern
63
+
64
+ if scheme_name = colors_obj.to_scheme(*@args)
65
+ opts[:color_scheme] = scheme_name
66
+ end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ def colors_obj
73
+ Logsly.colors(self.colors)
74
+ end
75
+
76
+ end
77
+
78
+ ## STDOUT
79
+
80
+ class Stdout < Base
81
+
82
+ def to_appender(*args)
83
+ data = BaseData.new(*args, &self.build)
84
+ Logging.appenders.stdout(:layout => self.to_layout(data))
85
+ end
86
+
87
+ end
88
+
89
+ ## FILE
90
+
91
+ class File < Base
92
+
93
+ def to_appender(*args)
94
+ data = FileData.new(*args, &self.build)
95
+ Logging.appenders.file(data.path, :layout => self.to_layout(data))
96
+ end
97
+
98
+ end
99
+
100
+ class FileData < BaseData
101
+
102
+ def path(value = nil)
103
+ @path = value if !value.nil?
104
+ @path
105
+ end
106
+
107
+ end
108
+
109
+ ## SYSLOG
110
+
111
+ class Syslog < Base
112
+
113
+ def to_appender(*args)
114
+ ::Syslog.close if ::Syslog.opened?
115
+
116
+ data = SyslogData.new(*args, &self.build)
117
+ Logging.appenders.syslog(data.identity, {
118
+ :logopt => data.log_opts,
119
+ :facility => data.facility,
120
+ :layout => self.to_layout(data)
121
+ })
122
+ end
123
+
124
+ end
125
+
126
+ class SyslogData < BaseData
127
+
128
+ def initialize(*args, &build)
129
+ super
130
+ @log_opts = (::Syslog::LOG_PID | ::Syslog::LOG_CONS)
131
+ @facility = ::Syslog::LOG_LOCAL0
132
+ end
133
+
134
+ def identity(value = nil)
135
+ @identity = value if !value.nil?
136
+ @identity
137
+ end
138
+
139
+ def log_opts(value = nil)
140
+ @log_opts = value if !value.nil?
141
+ @log_opts
142
+ end
143
+
144
+ def facility(value = nil)
145
+ @facility = value if !value.nil?
146
+ @facility
147
+ end
148
+
149
+ end
150
+
151
+ end
@@ -1,3 +1,3 @@
1
1
  module Logsly
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/logsly.rb CHANGED
@@ -1,63 +1,57 @@
1
- require 'ns-options'
1
+ require 'much-plugin'
2
2
  require 'logging'
3
3
  require 'logsly/version'
4
4
  require 'logsly/colors'
5
- require 'logsly/base_output'
5
+ require 'logsly/outputs'
6
6
 
7
7
  module Logsly
8
+ include MuchPlugin
8
9
 
9
- def self.included(receiver)
10
- receiver.class_eval do
11
- attr_reader :log_type, :level, :outputs, :logger
12
- include LoggerMethods
13
- end
14
- end
15
-
16
- module Settings
17
- include NsOptions::Proxy
10
+ plugin_included do
11
+ include InstanceMethods
18
12
 
19
- option :colors, ::Hash, :default => ::Hash.new(NullColors.new)
20
- option :outputs, ::Hash, :default => ::Hash.new(NullOutput.new)
21
13
  end
22
14
 
23
15
  def self.reset
24
- Settings.reset
16
+ @settings = nil
25
17
  Logging.reset
26
18
  end
27
19
 
20
+ def self.settings
21
+ @settings ||= Settings.new
22
+ end
23
+
28
24
  def self.colors(name, &block)
29
- require 'logsly/colors'
30
- Settings.colors[name.to_s] = Colors.new(name, &block) if !block.nil?
31
- Settings.colors[name.to_s]
25
+ settings.colors[name.to_s] = Colors.new(name, &block) if !block.nil?
26
+ settings.colors[name.to_s]
32
27
  end
33
28
 
34
29
  def self.stdout(name, &block)
35
- require 'logsly/stdout_output'
36
- Settings.outputs[name.to_s] = StdoutOutput.new(&block)
30
+ settings.outputs[name.to_s] = Outputs::Stdout.new(&block)
37
31
  end
38
32
 
39
33
  def self.file(name, &block)
40
- require 'logsly/file_output'
41
- Settings.outputs[name.to_s] = FileOutput.new(&block)
34
+ settings.outputs[name.to_s] = Outputs::File.new(&block)
42
35
  end
43
36
 
44
37
  def self.syslog(name, &block)
45
- require 'logsly/syslog_output'
46
- Settings.outputs[name.to_s] = SyslogOutput.new(&block)
38
+ settings.outputs[name.to_s] = Outputs::Syslog.new(&block)
47
39
  end
48
40
 
49
41
  def self.outputs(name)
50
- Settings.outputs[name.to_s]
42
+ settings.outputs[name.to_s]
51
43
  end
52
44
 
53
- module LoggerMethods
45
+ module InstanceMethods
54
46
 
55
- def initialize(log_type, opts_hash=nil)
56
- opts = NsOptions::Struct.new(opts_hash) do
57
- option :level, String, :default => 'info'
58
- option :outputs, Array, :default => []
59
- end
60
- @log_type, @level, @outputs = log_type.to_s, opts.level, opts.outputs
47
+ attr_reader :log_type, :level, :outputs, :logger
48
+
49
+ def initialize(log_type, opts = nil)
50
+ opts ||= {}
51
+
52
+ @log_type = log_type.to_s
53
+ @level = (opts[:level] || opts['level'] || 'info').to_s
54
+ @outputs = opts[:outputs] || opts['outputs'] || []
61
55
 
62
56
  unique_name = "#{self.class.name}-#{@log_type}-#{self.object_id}"
63
57
  @logger = Logging.logger[unique_name]
@@ -119,4 +113,13 @@ module Logsly
119
113
 
120
114
  end
121
115
 
116
+ class Settings
117
+ attr_reader :colors, :outputs
118
+
119
+ def initialize
120
+ @colors = ::Hash.new(NullColors.new)
121
+ @outputs = ::Hash.new(Outputs::Null.new)
122
+ end
123
+ end
124
+
122
125
  end
data/logsly.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Logsly::VERSION
9
9
  gem.authors = ["Kelly Redding", "Collin Redding"]
10
10
  gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
- gem.description = %q{Create custom loggers.}
12
11
  gem.summary = %q{Create custom loggers.}
12
+ gem.description = %q{Create custom loggers.}
13
13
  gem.homepage = "http://github.com/redding/logsly"
14
14
  gem.license = 'MIT'
15
15
 
@@ -18,9 +18,9 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_development_dependency("assert", ["~> 2.15"])
21
+ gem.add_development_dependency("assert", ["~> 2.16.1"])
22
22
 
23
- gem.add_dependency("ns-options", ["~> 1.1"])
23
+ gem.add_dependency("much-plugin", ["~> 0.2.0"])
24
24
  gem.add_dependency("logging", ["~> 1.7"])
25
25
 
26
26
  end
@@ -22,7 +22,7 @@ class Logsly::Colors
22
22
  end
23
23
 
24
24
  should "know its build" do
25
- build_proc = Proc.new {}
25
+ build_proc = Proc.new{}
26
26
  out = Logsly::Colors.new 'test', &build_proc
27
27
 
28
28
  assert_same build_proc, out.build
@@ -90,21 +90,21 @@ class Logsly::Colors
90
90
  method_name :white
91
91
  info :blue
92
92
  end
93
- expected = {
93
+ exp = {
94
94
  :date => :blue,
95
95
  :message => :cyan,
96
96
  :method => :white,
97
97
  :levels => {:info => :blue},
98
98
  }
99
99
 
100
- assert_equal expected, general_only.to_scheme_opts
100
+ assert_equal exp, general_only.to_scheme_opts
101
101
  end
102
102
 
103
103
  should "only include :levels and :lines if at least one is set" do
104
104
  no_levels_lines = Logsly::ColorsData.new { date :blue }
105
- expected = { :date => :blue }
105
+ exp = { :date => :blue }
106
106
 
107
- assert_equal expected, no_levels_lines.to_scheme_opts
107
+ assert_equal exp, no_levels_lines.to_scheme_opts
108
108
  end
109
109
 
110
110
  end
@@ -2,6 +2,8 @@ require 'assert'
2
2
  require 'logsly'
3
3
 
4
4
  require 'logging'
5
+ require 'logsly/colors'
6
+ require 'logsly/outputs'
5
7
  require 'test/support/logger'
6
8
 
7
9
  module Logsly
@@ -20,7 +22,7 @@ module Logsly
20
22
  end
21
23
 
22
24
  should "return a NullOutput obj when requesting an output that isn't defined" do
23
- assert_kind_of NullOutput, Logsly.outputs('not_defined_yet')
25
+ assert_kind_of Outputs::Null, Logsly.outputs('not_defined_yet')
24
26
  end
25
27
 
26
28
  should "add a named color scheme using the `colors` method" do
@@ -31,27 +33,27 @@ module Logsly
31
33
  end
32
34
 
33
35
  should "add a named stdout output using the `stdout` method" do
34
- assert_kind_of NullOutput, Logsly.outputs('test_stdout')
36
+ assert_kind_of Outputs::Null, Logsly.outputs('test_stdout')
35
37
  Logsly.stdout('test_stdout') {}
36
38
 
37
39
  assert_not_nil Logsly.outputs('test_stdout')
38
- assert_kind_of StdoutOutput, Logsly.outputs('test_stdout')
40
+ assert_kind_of Outputs::Stdout, Logsly.outputs('test_stdout')
39
41
  end
40
42
 
41
43
  should "add a named file output using the `file` method" do
42
- assert_kind_of NullOutput, Logsly.outputs('test_file')
44
+ assert_kind_of Outputs::Null, Logsly.outputs('test_file')
43
45
  Logsly.file('test_file') {}
44
46
 
45
47
  assert_not_nil Logsly.outputs('test_file')
46
- assert_kind_of FileOutput, Logsly.outputs('test_file')
48
+ assert_kind_of Outputs::File, Logsly.outputs('test_file')
47
49
  end
48
50
 
49
51
  should "add a named syslog output using the `syslog` method" do
50
- assert_kind_of NullOutput, Logsly.outputs('test_syslog')
52
+ assert_kind_of Outputs::Null, Logsly.outputs('test_syslog')
51
53
  Logsly.syslog('test_syslog') {}
52
54
 
53
55
  assert_not_nil Logsly.outputs('test_syslog')
54
- assert_kind_of SyslogOutput, Logsly.outputs('test_syslog')
56
+ assert_kind_of Outputs::Syslog, Logsly.outputs('test_syslog')
55
57
  end
56
58
 
57
59
  should "convert non-string setting names to string" do
@@ -71,10 +73,10 @@ module Logsly
71
73
 
72
74
  should "overwrite same-named outputs settings" do
73
75
  Logsly.stdout('something') {}
74
- assert_kind_of StdoutOutput, Logsly.outputs('something')
76
+ assert_kind_of Outputs::Stdout, Logsly.outputs('something')
75
77
 
76
78
  Logsly.file('something') {}
77
- assert_kind_of FileOutput, Logsly.outputs('something')
79
+ assert_kind_of Outputs::File, Logsly.outputs('something')
78
80
  end
79
81
 
80
82
  end
@@ -86,14 +88,14 @@ module Logsly
86
88
  Logsly.stdout('test_stdout') {}
87
89
  end
88
90
 
89
- should "reset the Settings" do
90
- assert_kind_of Colors, Logsly.colors('test_colors')
91
- assert_kind_of StdoutOutput, Logsly.outputs('test_stdout')
91
+ should "reset the settings" do
92
+ assert_kind_of Colors, Logsly.colors('test_colors')
93
+ assert_kind_of Outputs::Stdout, Logsly.outputs('test_stdout')
92
94
 
93
95
  Logsly.reset
94
96
 
95
- assert_kind_of NullColors, Logsly.colors('test_colors')
96
- assert_kind_of NullOutput, Logsly.outputs('test_stdout')
97
+ assert_kind_of NullColors, Logsly.colors('test_colors')
98
+ assert_kind_of Outputs::Null, Logsly.outputs('test_stdout')
97
99
  end
98
100
 
99
101
  end
@@ -129,8 +131,8 @@ module Logsly
129
131
  end
130
132
 
131
133
  should "create the Logging::Logger with a unique name" do
132
- expected = "#{subject.class.name}-testy_log_logger-#{subject.object_id}"
133
- assert_equal expected, subject.logger.name
134
+ exp = "#{subject.class.name}-testy_log_logger-#{subject.object_id}"
135
+ assert_equal exp, subject.logger.name
134
136
  end
135
137
 
136
138
  should "set the logger's level" do