slf4r 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2008-12-19
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/log4r_adapter.rb
6
+ lib/logging_adapter.rb
7
+ lib/ruby_logger_adapter.rb
8
+ lib/slf4r.rb
9
+ lib/slf4r/abstract_logger_facade.rb
10
+ lib/slf4r/logger.rb
11
+ lib/slf4r/logging_logger.rb
12
+ lib/slf4r/noop_logger.rb
13
+ lib/slf4r/ruby_logger.rb
14
+ lib/slf4r/version.rb
15
+ lib/slf4r/wrapped_logger.rb
16
+ test/test_slf4r.rb
data/README.txt ADDED
@@ -0,0 +1,99 @@
1
+ = slf4r
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ the main idea is from www.slf4j.org which is to provide a uniform interface for instantiating und using of a logger. but the actual logging is done by some third party logging framework.
8
+
9
+ one idea is to have a logger per class or object (see ). in ruby you would have something like
10
+
11
+ @logger = Slf4r::LoggerFacade.new(self.class)
12
+
13
+ or the convinience module
14
+
15
+ include Slf4r::Logger
16
+
17
+ if the underlying logging framework allows it (like logging or log4r) then you get a logger for each namespace of your class and create a hierachy of loggers. with this you can control the log level for each logger and/or namespace.
18
+
19
+ for example you have a framework A with namespace 'A' then you can set the log level for the logger with name 'A' to debug and get all the debug from the framework, etc.
20
+
21
+ in case you have a framework B which uses log4r internally you can use the 'log4r_adapter' to delegate the logger creation from log4r to slf4r. in this way you have only one place where logging gets configured and controlled.
22
+
23
+ == FEATURES/PROBLEMS:
24
+
25
+ * can replace other logging frameworks via adapters
26
+
27
+ * for the actual logging it depends on a third party logging framework
28
+
29
+ == SYNOPSIS:
30
+
31
+ === using with logging gem
32
+
33
+ require 'slf4r/logging_logger'
34
+
35
+ Logging.init :debug, :info, :warn, :error
36
+
37
+ appender = Logging::Appender.stdout
38
+ appender.layout = Logging::Layouts::Pattern.new(:pattern => "%d [%-l] (%c) %m\n")
39
+ logger = Logging::Logger.new(:root)
40
+ logger.add_appenders(appender)
41
+ logger.level = :debug
42
+ logger.info "initialized logger . . ."
43
+
44
+ === using with ruby logger
45
+
46
+ require 'slf4r/ruby_logger'
47
+
48
+ === using the log4r adapter
49
+
50
+ require 'log4r_adapter'
51
+
52
+ === using with rails/merb/datamapper logger
53
+
54
+ require 'slf4r/wrappered_logger'
55
+
56
+ LoggerFacade4WrappedLogger.logger(framwork_logger)
57
+
58
+ === getting an instance of a logger
59
+
60
+ Slf4r::LoggerFacade.new("Full::Qualified::Class::Name")
61
+
62
+ == REQUIREMENTS:
63
+
64
+ * logging for slf4r/logging_logger
65
+
66
+ * log4r for slf4r/log4r_logger
67
+
68
+ == INSTALL:
69
+
70
+ * sudo gem install slf4r
71
+
72
+ * sudo gem install logging # optional
73
+
74
+ * sudo gem install log4r # optional
75
+
76
+ == LICENSE:
77
+
78
+ (The MIT License)
79
+
80
+ Copyright (c) 2009 kristian meier
81
+
82
+ Permission is hereby granted, free of charge, to any person obtaining
83
+ a copy of this software and associated documentation files (the
84
+ 'Software'), to deal in the Software without restriction, including
85
+ without limitation the rights to use, copy, modify, merge, publish,
86
+ distribute, sublicense, and/or sell copies of the Software, and to
87
+ permit persons to whom the Software is furnished to do so, subject to
88
+ the following conditions:
89
+
90
+ The above copyright notice and this permission notice shall be
91
+ included in all copies or substantial portions of the Software.
92
+
93
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
94
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
95
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
96
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
97
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
98
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
99
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/slf4r/version.rb'
6
+
7
+ Hoe.new('slf4r', Slf4r::VERSION) do |p|
8
+ p.rubyforge_name = 'slf4r' # if different than lowercase project name
9
+ p.summary = 'Slf4r provides a uniform interface for instantiating und using of a logger'
10
+ p.url = 'http://slf4r.rubyforge.com/'
11
+ p.developer('mkristian', 'm.kristian@web.de')
12
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
13
+ p.remote_rdoc_dir = '' # Release to root
14
+ end
15
+
16
+ desc 'Install the package as a gem.'
17
+ task :install => [:clean, :package] do
18
+ gem = Dir['pkg/*.gem'].first
19
+ sh "gem install --local #{gem} --no-ri --no-rdoc"
20
+ end
21
+
22
+ # vim: syntax=Ruby
@@ -0,0 +1,12 @@
1
+ module Log4r
2
+ class Logger
3
+
4
+ def initialize(_fullname, _level=nil, _additive=true, _trace=false)
5
+ @logger = ::Slf4r::LoggerFacade.new(_fullname)
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ @logger.send(method, *args, &block) if @logger.respond_to?(method)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Logging
2
+ class Logger
3
+
4
+ def initialize(name)
5
+ @logger = ::Slf4r::LoggerFacade.new(name)
6
+ end
7
+
8
+ class << self
9
+ alias :[] :new
10
+ end
11
+
12
+ def method_missing(method, *args, &block)
13
+ @logger.send(method, *args, &block) if @logger.respond_to?(method)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ class Logger
2
+
3
+ def initialize(*args)
4
+ @logger = ::Slf4r::LoggerFacade.new(:root)
5
+ end
6
+
7
+ def method_missing(method, *args, &block)
8
+ @logger.send(method, *args, &block) if @logger.respond_to?(method)
9
+ end
10
+ end
@@ -0,0 +1,70 @@
1
+ module Slf4r
2
+ class AbstractLoggerFacade
3
+
4
+ protected
5
+
6
+ def format(exception)
7
+ ": #{exception.message}:\n\t#{exception.backtrace.join('\n\t')}" if exception
8
+ end
9
+
10
+ def _debug(msg)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def _info(msg)
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def _warn(msg)
19
+ raise NotImplementedError
20
+ end
21
+
22
+ def _error(msg)
23
+ raise NotImplementedError
24
+ end
25
+
26
+ public
27
+
28
+ attr_reader :name
29
+
30
+ def initialize(name)
31
+ @name = name
32
+ end
33
+
34
+ def debug?
35
+ raise NotImplementedError
36
+ end
37
+
38
+ def debug(msg = nil, exception = nil)
39
+ msg, exception = yield if block_given?
40
+ _debug("#{msg}#{format(exception)}")
41
+ end
42
+
43
+ def info?
44
+ raise NotImplementedError
45
+ end
46
+
47
+ def info(msg = nil, exception = nil)
48
+ msg, exception = yield if block_given?
49
+ _info("#{msg}#{format(exception)}")
50
+ end
51
+
52
+ def warn?
53
+ raise NotImplementedError
54
+ end
55
+
56
+ def warn(msg = nil, exception = nil)
57
+ msg, exception = yield if block_given?
58
+ _warn("#{msg}#{format(exception)}")
59
+ end
60
+
61
+ def error?
62
+ raise NotImplementedError
63
+ end
64
+
65
+ def error(msg = nil, exception = nil)
66
+ msg, exception = yield if block_given?
67
+ _error("#{msg}#{format(exception)}")
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ module Slf4r
2
+ module Logger
3
+
4
+ def logger
5
+ @logger ||= LoggerFacade.new(self.class == Class ? name : self.class)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,63 @@
1
+ require 'logging'
2
+ require 'slf4r/abstract_logger_facade'
3
+
4
+ module Slf4r
5
+ class LoggerFacade < AbstractLoggerFacade
6
+
7
+ attr_reader :logger
8
+
9
+ def initialize(name)
10
+ super
11
+ log_name = nil
12
+ name.to_s.split("::").each do |n|
13
+ if log_name
14
+ log_name += "::#{n}"
15
+ else
16
+ log_name = n
17
+ end
18
+ @logger = ::Logging::Logger.new(log_name)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def log(type, msg)
25
+ @logger.add(type, msg)
26
+ end
27
+
28
+ public
29
+
30
+ def debug?
31
+ @logger.level == 0
32
+ end
33
+
34
+ def _debug(msg)
35
+ log(0, msg)
36
+ end
37
+
38
+ def info?
39
+ @logger.level <= 1
40
+ end
41
+
42
+ def _info(msg)
43
+ log(1, msg)
44
+ end
45
+
46
+ def warn?
47
+ @logger.level <= 2
48
+ end
49
+
50
+ def _warn(msg)
51
+ log(2, msg)
52
+ end
53
+
54
+ def error?
55
+ @logger.level <= 3
56
+ end
57
+
58
+ def _error(msg)
59
+ log(3, msg)
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,40 @@
1
+ module Slf4r
2
+ class LoggerFacade
3
+
4
+ def initialize(name)
5
+ @name = name
6
+ end
7
+
8
+ public
9
+
10
+ attr_reader :name
11
+
12
+ def debug?
13
+ false
14
+ end
15
+
16
+ def debug(msg = nil, exception = nil)
17
+ end
18
+
19
+ def info?
20
+ false
21
+ end
22
+
23
+ def info(msg = nil, exception = nil)
24
+ end
25
+
26
+ def warn?
27
+ false
28
+ end
29
+
30
+ def warn(msg = nil, exception = nil)
31
+ end
32
+
33
+ def error?
34
+ false
35
+ end
36
+
37
+ def error(msg = nil, exception = nil)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,80 @@
1
+ require 'logger.rb'
2
+ require 'slf4r/abstract_logger_facade'
3
+
4
+ module Slf4r
5
+ class LoggerFacade4RubyLogger
6
+
7
+ @@level = ::Logger::INFO
8
+ @@file = STDERR
9
+ @@datetime_format = "%Y-%m-%d %H:%M:%S "
10
+
11
+ def self.new_logger_facade(name)
12
+ @name = name
13
+ @logger = ::Logger.new(@@file)
14
+ @logger.level = @@level
15
+ @logger.datetime_format = @@datetime_format
16
+ @logger
17
+ end
18
+
19
+ def self.level=(level)
20
+ @@level = level.instance_of?(Fixnum) ? level :
21
+ ::Logger.get_const(level.to_s.upcase)
22
+ end
23
+
24
+ def self.datetime_format=(format)
25
+ @@datetime_format = format
26
+ end
27
+
28
+ def self.file=(file)
29
+ @@file = file
30
+ end
31
+ end
32
+
33
+ class LoggerFacade < AbstractLoggerFacade
34
+
35
+ def initialize(name)
36
+ super
37
+ @logger = LoggerFacade4RubyLogger.new_logger_facade(name)
38
+ end
39
+
40
+ private
41
+
42
+ def log(type, msg)
43
+ @logger.add(type, msg, @name)
44
+ end
45
+
46
+ public
47
+
48
+ def debug?
49
+ @logger.debug?
50
+ end
51
+
52
+ def _debug(msg)
53
+ log(::Logger::DEBUG, msg)
54
+ end
55
+
56
+ def info?
57
+ @logger.info?
58
+ end
59
+
60
+ def _info(msg)
61
+ log(::Logger::INFO, msg)
62
+ end
63
+
64
+ def warn?
65
+ @logger.warn?
66
+ end
67
+
68
+ def _warn(msg)
69
+ log(::Logger::WARN, msg)
70
+ end
71
+
72
+ def error?
73
+ @logger.error?
74
+ end
75
+
76
+ def _error(msg)
77
+ log(::Logger::ERROR, msg)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Slf4r
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'abstract_logger_facade'
2
+ module Slf4r
3
+ class LoggerFacade4WrappedLogger
4
+
5
+ @@logger = nil
6
+
7
+ def self.new_logger_facade(name)
8
+ raise "set logger first" unless @@logger
9
+ @@logger
10
+ end
11
+
12
+ def self.logger=(logger)
13
+ @@logger = logger
14
+ end
15
+ end
16
+
17
+ class LoggerFacade < AbstractLoggerFacade
18
+
19
+ def initialize(name)
20
+ super
21
+ @logger = LoggerFacade4WrappedLogger.new_logger_facade(name)
22
+ end
23
+
24
+ protected
25
+
26
+ def _debug(msg)
27
+ @logger.debug(msg)
28
+ end
29
+
30
+ def _info(msg)
31
+ @logger.info(msg)
32
+ end
33
+
34
+ def _warn(msg)
35
+ @logger.warn(msg)
36
+ end
37
+
38
+ def _error(msg)
39
+ @logger.error(msg)
40
+ end
41
+
42
+ public
43
+
44
+ def debug?
45
+ @logger.debug?
46
+ end
47
+
48
+ def info?
49
+ @logger.info?
50
+ end
51
+
52
+ def warn?
53
+ @logger.warn?
54
+ end
55
+
56
+ def error?
57
+ @logger.error?
58
+ end
59
+ end
60
+ end
data/lib/slf4r.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'pathname'
3
+
4
+ dir = Pathname(__FILE__).dirname.expand_path + 'slf4r'
5
+
6
+ require dir + 'version'
7
+ require dir + 'logger'
8
+
File without changes
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slf4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mkristian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-08 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ description: the main idea is from www.slf4j.org which is to provide a uniform interface for instantiating und using of a logger. but the actual logging is done by some third party logging framework. one idea is to have a logger per class or object (see ). in ruby you would have something like @logger = Slf4r::LoggerFacade.new(self.class) or the convinience module include Slf4r::Logger if the underlying logging framework allows it (like logging or log4r) then you get a logger for each namespace of your class and create a hierachy of loggers. with this you can control the log level for each logger and/or namespace. for example you have a framework A with namespace 'A' then you can set the log level for the logger with name 'A' to debug and get all the debug from the framework, etc. in case you have a framework B which uses log4r internally you can use the 'log4r_adapter' to delegate the logger creation from log4r to slf4r. in this way you have only one place where logging gets configured and controlled.
26
+ email:
27
+ - m.kristian@web.de
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - lib/log4r_adapter.rb
42
+ - lib/logging_adapter.rb
43
+ - lib/ruby_logger_adapter.rb
44
+ - lib/slf4r.rb
45
+ - lib/slf4r/abstract_logger_facade.rb
46
+ - lib/slf4r/logger.rb
47
+ - lib/slf4r/logging_logger.rb
48
+ - lib/slf4r/noop_logger.rb
49
+ - lib/slf4r/ruby_logger.rb
50
+ - lib/slf4r/version.rb
51
+ - lib/slf4r/wrapped_logger.rb
52
+ - test/test_slf4r.rb
53
+ has_rdoc: true
54
+ homepage: http://slf4r.rubyforge.com/
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --main
58
+ - README.txt
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: slf4r
76
+ rubygems_version: 1.2.0
77
+ signing_key:
78
+ specification_version: 2
79
+ summary: Slf4r provides a uniform interface for instantiating und using of a logger
80
+ test_files:
81
+ - test/test_slf4r.rb