easy_log4r 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/easy_log4r/easy_logger.rb +8 -0
- data/lib/easy_log4r/logger.rb +59 -5
- data/lib/easy_log4r/null_logger.rb +8 -0
- data/lib/easy_log4r.rb +9 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -1,4 +1,12 @@
|
|
1
|
+
##
|
2
|
+
# EasyLogger
|
3
|
+
#
|
4
|
+
# This class is what sets up the EasyLog4r Logger in your class when you include it in the class.
|
5
|
+
#
|
1
6
|
module EasyLog4r::EasyLogger
|
7
|
+
|
8
|
+
##
|
9
|
+
# Included hook which calls EasyLog4r::Logger#add_logger
|
2
10
|
def self.included(base)
|
3
11
|
EasyLog4r::Logger.add_logger(base, EasyLog4r::Logger.get_logger(base))
|
4
12
|
end
|
data/lib/easy_log4r/logger.rb
CHANGED
@@ -1,21 +1,71 @@
|
|
1
|
+
##
|
2
|
+
# Class that creates Log4r::Loggers with some basic defaults and also modifies base classes to referece said Loggers.
|
3
|
+
#
|
1
4
|
class EasyLog4r::Logger
|
2
5
|
require 'log4r'
|
3
6
|
include Log4r
|
4
7
|
|
8
|
+
##
|
9
|
+
# Adds log4r class variable, as well as Base::log4r and Base#log4r
|
5
10
|
def self.add_logger(base, logger)
|
6
11
|
base.class_variable_set(:@@log4r, logger)
|
7
|
-
base.
|
8
|
-
|
12
|
+
base.class_eval do
|
13
|
+
def log4r; class_variable_get(:@@log4r); end
|
14
|
+
def self.log4r; class_variable_get(:@@log4r); end
|
15
|
+
end
|
9
16
|
end
|
10
17
|
|
18
|
+
##
|
19
|
+
# Provides the default Loggers, which are null_loggers with the default outputters.
|
11
20
|
def self.default_logger(name)
|
12
21
|
logger = null_logger(name)
|
13
|
-
|
14
|
-
|
15
|
-
Log4r::StderrOutputter.new(:formatter => formatter)]
|
22
|
+
logger.outputters = default_outputters
|
23
|
+
|
16
24
|
return logger
|
17
25
|
end
|
18
26
|
|
27
|
+
##
|
28
|
+
# Creates or returns default outputters.
|
29
|
+
#
|
30
|
+
# Those outputters are a Log4r::StdoutOutputter and a Log4r::StderrOutputter, both of which have a default Log4r::Formatter
|
31
|
+
def self.default_outputters
|
32
|
+
return @outputters if @outputters
|
33
|
+
@outputters = []
|
34
|
+
formatter = default_formatter
|
35
|
+
|
36
|
+
if out = Log4r::Outputter['formatted_stdout']
|
37
|
+
@outputters << out
|
38
|
+
else
|
39
|
+
@outputters << Log4r::StdoutOutputter.new('formatted_stdout', :formatter => formatter)
|
40
|
+
end
|
41
|
+
|
42
|
+
if out = Log4r::Outputter['formatted_stderr']
|
43
|
+
@outputters << out
|
44
|
+
else
|
45
|
+
@outputters << Log4r::StderrOutputter.new('formatted_stderr', :formatter => formatter)
|
46
|
+
end
|
47
|
+
|
48
|
+
return @outputters
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# Creates or returns the default formatter.
|
53
|
+
#
|
54
|
+
# The formatter is a Log4r::PatternFormatter with the following pattern:
|
55
|
+
# "[%l] %d @@%c :: %m"
|
56
|
+
#
|
57
|
+
# For example:
|
58
|
+
# Base.log4r.info("This is only a test")
|
59
|
+
# => [INFO] 2012-07-30 12:00:00 PM @@Base :: This is only a test
|
60
|
+
def self.default_formatter
|
61
|
+
return @formatter if @formatter
|
62
|
+
@formatter = Log4r::PatternFormatter.new(:pattern => "[%l] %d @@%c :: %m")
|
63
|
+
|
64
|
+
return @formatter
|
65
|
+
end
|
66
|
+
|
67
|
+
##
|
68
|
+
# Creates a Log4r::Logger with the given name and clears all Outputters
|
19
69
|
def self.null_logger(name)
|
20
70
|
logger = Log4r::Logger.new(name)
|
21
71
|
logger.outputters = []
|
@@ -23,6 +73,10 @@ class EasyLog4r::Logger
|
|
23
73
|
return logger
|
24
74
|
end
|
25
75
|
|
76
|
+
##
|
77
|
+
# Given a Class, this will return either an existing Log4r::Logger, that could be defined via XML or YAML options, or a fresh new logger, that has the name of the class, as represented by Class.to_s, as its name.
|
78
|
+
#
|
79
|
+
# If the second option is given any value which resolves to true, it will provide a Log4r::Logger with no Outputters.
|
26
80
|
def self.get_logger(base, null=false)
|
27
81
|
clas = base.to_s
|
28
82
|
logger = null ? null_logger(clas) : Log4r::Logger[clas]
|
@@ -1,4 +1,12 @@
|
|
1
|
+
##
|
2
|
+
# Creates a Log4rLogger with no Outputters, or overwrites the default Logger provided by EasyLog4r::EasyLogger, removing it's Outputters
|
3
|
+
#
|
4
|
+
# This enables you to keep your log4r statements in your code without producing any output whatsoever.
|
5
|
+
#
|
1
6
|
module EasyLog4r::NullLogger
|
7
|
+
|
8
|
+
##
|
9
|
+
# Included hook which nullifies the default Logger by removing its Outputters
|
2
10
|
def self.included(base)
|
3
11
|
EasyLog4r::Logger.add_logger(base, EasyLog4r::Logger.get_logger(base, :null))
|
4
12
|
end
|
data/lib/easy_log4r.rb
CHANGED
@@ -1,4 +1,12 @@
|
|
1
|
-
|
1
|
+
##
|
2
|
+
# Base Class for EasyLog4r. Standard includes for the rest of the gem.
|
3
|
+
#
|
4
|
+
# To use EasyLog4r, just include the gem in your Gemfile, then drop the following into your class:
|
5
|
+
# include EasyLog4r::EasyLogger
|
6
|
+
#
|
7
|
+
# This produces a log4r method on the class and any instance that can be used to access a Log4r::Logger with some simple defaults.
|
8
|
+
#
|
9
|
+
module EasyLog4r
|
2
10
|
require 'easy_log4r/logger'
|
3
11
|
require 'easy_log4r/easy_logger'
|
4
12
|
require 'easy_log4r/null_logger'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_log4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -146,7 +146,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
146
146
|
version: '0'
|
147
147
|
segments:
|
148
148
|
- 0
|
149
|
-
hash:
|
149
|
+
hash: 295757693681432550
|
150
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
151
|
none: false
|
152
152
|
requirements:
|