globalog 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/globalog.rb +37 -9
- data/lib/globalog_args.rb +6 -0
- data/test/external_helper.rb +16 -3
- data/test/tc_globalog_main.rb +6 -2
- metadata +2 -2
data/lib/globalog.rb
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
#
|
8
8
|
# It is essentially compatible with existing Logger, because it uses logger as it base lib. So all logging is done the same way, with all of logger instance methods also working on globalog instances. You can then transition code logged with logger easily, by either replacing the Logger initialisation in your code by a GlobaLog initialisation, or by leaving logger where it is and only deriving its output and level settings from globalog class methods of the same names, still allowing you to tap into globalog cascading settings.
|
9
9
|
#
|
10
|
-
# The logging in
|
10
|
+
# The logging in GlobaLog is done with Logger so please refer to its documentation for the logging methodologies: http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/
|
11
11
|
#
|
12
12
|
# ///////////////////////////////////////////////////////////////////////////////////////
|
13
13
|
# Example:
|
@@ -51,7 +51,7 @@
|
|
51
51
|
# => Printing Debug!!
|
52
52
|
#
|
53
53
|
# So this allow you to set a default Logger setting in your libraries, override them with an other default setting in client scripts and still get the flexibility to override both defaults at runtime by providing command line arguments. Ideal for testing purposes where you want a default log level for test overriding the Local log level of your tested libraries, while allowing you to choose the test log level at runtime.
|
54
|
-
#
|
54
|
+
# See Logger rdoc for full operation details: http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/
|
55
55
|
#
|
56
56
|
# Author:: lp (mailto:lp@spiralix.org)
|
57
57
|
# Copyright:: 2009 Louis-Philippe Perron - Released under the terms of the MIT license
|
@@ -63,10 +63,11 @@ class GlobaLog < Logger
|
|
63
63
|
require 'globalog_args'
|
64
64
|
require 'globalog_hijack'
|
65
65
|
|
66
|
-
# The
|
66
|
+
# The GlobaLog.logger acts as a constructor method for the logger.
|
67
67
|
# === Parameters
|
68
68
|
# * _def_out_ = the output io or logfile path
|
69
69
|
# * _def_level_ = the log level, as a symbol (i.e. :warn, :info, etc)
|
70
|
+
# * _opt1_, _opt2_ = (optional) logger extra log rotation parameters (see Logger rdoc for more details)
|
70
71
|
# * _master_ = (optional) the logger presence in the cascading settings chain
|
71
72
|
# _master_ can be any of the following:
|
72
73
|
# * _true_ = Impose its settings on the loggers down the chain
|
@@ -75,19 +76,29 @@ class GlobaLog < Logger
|
|
75
76
|
# === Example
|
76
77
|
# @log = GlobaLog.logger('logfile.txt',:error,true)
|
77
78
|
|
78
|
-
def GlobaLog.logger(def_out,def_level,
|
79
|
-
|
79
|
+
def GlobaLog.logger(def_out,def_level, opt1= :empty, opt2= :empty, opt3= :empty)
|
80
|
+
logger_opts = []; master = false
|
81
|
+
[opt1,opt2,opt3].each do |opt|
|
82
|
+
if opt == :empty
|
83
|
+
next
|
84
|
+
elsif opt == true || opt == false || opt == nil
|
85
|
+
master = opt
|
86
|
+
else
|
87
|
+
logger_opts << opt
|
88
|
+
end
|
89
|
+
end
|
90
|
+
Args.are({:log_output => def_out, :log_level => def_level, :log_opts => logger_opts},master)
|
80
91
|
if master.nil?
|
81
|
-
log = self.new(def_out)
|
92
|
+
log = self.new(def_out,*logger_opts)
|
82
93
|
log.level = Args.sym_to_level(def_level)
|
83
94
|
else
|
84
|
-
log = self.new(Args::log_output)
|
95
|
+
log = self.new(Args::log_output,*logger_opts)
|
85
96
|
log.level = Args::log_level
|
86
97
|
end
|
87
98
|
return log
|
88
99
|
end
|
89
100
|
|
90
|
-
# The
|
101
|
+
# The GlobaLog::output method return the current log output.
|
91
102
|
# Can be used to chain existing Logger instances to GlobaLog cascade,
|
92
103
|
# when you don't want to replace them with GlobaLog instances.
|
93
104
|
# (and you can only replace the constructor, the logging will still work)
|
@@ -97,7 +108,7 @@ class GlobaLog < Logger
|
|
97
108
|
Args::log_output
|
98
109
|
end
|
99
110
|
|
100
|
-
# The
|
111
|
+
# The GlobaLog::level method return the current log level.
|
101
112
|
# Can be used to chain existing Logger instances to GlobaLog cascade,
|
102
113
|
# when you don't want to replace them with GlobaLog instances.
|
103
114
|
# (and you can only replace the constructor, the logging will still work)
|
@@ -107,4 +118,21 @@ class GlobaLog < Logger
|
|
107
118
|
Args::log_level
|
108
119
|
end
|
109
120
|
|
121
|
+
# The GlobaLog::options method return the current extra Logger options.
|
122
|
+
# i.e. Log rotations, size, etc.
|
123
|
+
# They are returned as array so must be splatted before they are usefull for classic Logger initialization.
|
124
|
+
# === Example
|
125
|
+
# $logger = Logger.new(GlobaLog::output,*GlobaLog::options)
|
126
|
+
def GlobaLog::options
|
127
|
+
Args::log_opts
|
128
|
+
end
|
129
|
+
|
130
|
+
# The GlobaLog::logger_params method return the current full Logger parameter arguments.
|
131
|
+
# They are returned as array so must be splatted before they are usefull for classic Logger initialization.
|
132
|
+
# === Example
|
133
|
+
# $logger = Logger.new(*GlobaLog::logger_params)
|
134
|
+
def GlobaLog::logger_params
|
135
|
+
return Args::log_output, *Args::log_opts
|
136
|
+
end
|
137
|
+
|
110
138
|
end
|
data/lib/globalog_args.rb
CHANGED
@@ -11,9 +11,11 @@ class GlobaLog
|
|
11
11
|
if override == true
|
12
12
|
$logger_args[:log_level] = args[:log_level].to_sym unless args[:log_level].nil?
|
13
13
|
$logger_args[:log_output] = args[:log_output] unless args[:log_output].nil?
|
14
|
+
$logger_args[:log_opts] = args[:log_opts] unless args[:log_opts].nil?
|
14
15
|
else
|
15
16
|
$logger_args[:log_level] ||= args[:log_level].to_sym unless args[:log_level].nil?
|
16
17
|
$logger_args[:log_output] ||= args[:log_output] unless args[:log_output].nil?
|
18
|
+
$logger_args[:log_opts] ||= args[:log_opts] unless args[:log_opts].nil?
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
@@ -28,6 +30,10 @@ class GlobaLog
|
|
28
30
|
$logger_args[:log_level]
|
29
31
|
end
|
30
32
|
|
33
|
+
def Args::log_opts
|
34
|
+
$logger_args[:log_opts]
|
35
|
+
end
|
36
|
+
|
31
37
|
def Args.sym_to_level(sym)
|
32
38
|
case sym
|
33
39
|
when :debug
|
data/test/external_helper.rb
CHANGED
@@ -12,14 +12,27 @@ module LogMessage
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class External
|
15
|
+
require 'logger'
|
15
16
|
include LogMessage
|
16
17
|
|
17
|
-
def initialize
|
18
|
-
|
18
|
+
def initialize(type)
|
19
|
+
case type
|
20
|
+
when 0
|
21
|
+
@log = GlobaLog.logger(STDERR,:warn)
|
22
|
+
@id = type
|
23
|
+
when 1
|
24
|
+
@log = Logger.new(*GlobaLog::logger_params)
|
25
|
+
@log.level = GlobaLog::level
|
26
|
+
@id = type
|
27
|
+
when 2
|
28
|
+
@log = Logger.new(GlobaLog::output,*GlobaLog::options)
|
29
|
+
@log.level = GlobaLog::level
|
30
|
+
@id = type
|
31
|
+
end
|
19
32
|
end
|
20
33
|
|
21
34
|
def log
|
22
|
-
log_all_level(
|
35
|
+
log_all_level("external #{@id}")
|
23
36
|
end
|
24
37
|
|
25
38
|
end
|
data/test/tc_globalog_main.rb
CHANGED
@@ -63,7 +63,9 @@ class TestGlobaLogMain < Test::Unit::TestCase
|
|
63
63
|
pid1 = fork {log_all_level('fork 1'); exit}
|
64
64
|
pid2 = fork {log_all_level('fork 2'); exit}
|
65
65
|
Process.waitpid(pid2,0)
|
66
|
-
External.new().log
|
66
|
+
External.new(0).log
|
67
|
+
External.new(1).log
|
68
|
+
External.new(2).log
|
67
69
|
end
|
68
70
|
|
69
71
|
def check_test(level)
|
@@ -76,7 +78,9 @@ class TestGlobaLogMain < Test::Unit::TestCase
|
|
76
78
|
assert_match(/base/,content)
|
77
79
|
assert_match(/fork 1/,content)
|
78
80
|
assert_match(/fork 2/,content)
|
79
|
-
assert_match(/external/,content)
|
81
|
+
assert_match(/external 0/,content)
|
82
|
+
assert_match(/external 1/,content)
|
83
|
+
assert_match(/external 2/,content)
|
80
84
|
end
|
81
85
|
|
82
86
|
def check_level(level,content)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: globalog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Louis-Philippe Perron
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-02-
|
12
|
+
date: 2009-02-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|