Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mkristian committed Feb 7, 2009
0 parents commit 55e4742
Show file tree
Hide file tree
Showing 35 changed files with 1,121 additions and 0 deletions.
6 changes: 6 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== 1.0.0 / 2008-12-19

* 1 major enhancement

* Birthday!

16 changes: 16 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
History.txt
Manifest.txt
README.txt
Rakefile
lib/log4r_adapter.rb
lib/logging_adapter.rb
lib/ruby_logger_adapter.rb
lib/slf4r.rb
lib/slf4r/abstract_logger_facade.rb
lib/slf4r/logger.rb
lib/slf4r/logging_logger.rb
lib/slf4r/noop_logger.rb
lib/slf4r/ruby_logger.rb
lib/slf4r/version.rb
lib/slf4r/wrapped_logger.rb
test/test_slf4r.rb
99 changes: 99 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
= slf4r

* FIX (url)

== 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.

== FEATURES/PROBLEMS:

* can replace other logging frameworks via adapters

* for the actual logging it depends on a third party logging framework

== SYNOPSIS:

=== using with logging gem

require 'slf4r/logging_logger'

Logging.init :debug, :info, :warn, :error

appender = Logging::Appender.stdout
appender.layout = Logging::Layouts::Pattern.new(:pattern => "%d [%-l] (%c) %m\n")
logger = Logging::Logger.new(:root)
logger.add_appenders(appender)
logger.level = :debug
logger.info "initialized logger . . ."

=== using with ruby logger

require 'slf4r/ruby_logger'

=== using the log4r adapter

require 'log4r_adapter'

=== using with rails/merb/datamapper logger

require 'slf4r/wrappered_logger'

LoggerFacade4WrappedLogger.logger(framwork_logger)

=== getting an instance of a logger

Slf4r::LoggerFacade.new("Full::Qualified::Class::Name")

== REQUIREMENTS:

* logging for slf4r/logging_logger

* log4r for slf4r/log4r_logger

== INSTALL:

* sudo gem install slf4r

* sudo gem install logging # optional

* sudo gem install log4r # optional

== LICENSE:

(The MIT License)

Copyright (c) 2009 kristian meier

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
99 changes: 99 additions & 0 deletions README.txt~
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
= slf4r

* FIX (url)

== DESCRIPTION:

the main ideas are from www.slf4j.org where the main idea is to provide a uniform interface for using instantiating und using 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.

== FEATURES/PROBLEMS:

* can replace other logging frameworks via adapters

* for the actual logging it depends on a third party logging framework

== SYNOPSIS:

=== using with logging gem

require 'slf4r/logging_logger'

Logging.init :debug, :info, :warn, :error

appender = Logging::Appender.stdout
appender.layout = Logging::Layouts::Pattern.new(:pattern => "%d [%-l] (%c) %m\n")
logger = Logging::Logger.new(:root)
logger.add_appenders(appender)
logger.level = :debug
logger.info "initialized logger . . ."

=== using with ruby logger

require 'slf4r/ruby_logger'

=== using the log4r adapter

require 'log4r_adapter'

=== using with rails/merb/datamapper logger

require 'slf4r/wrappered_logger'

LoggerFacade4WrappedLogger.logger(framwork_logger)

=== getting an instance of a logger

Slf4r::LoggerFacade.new("Full::Qualified::Class::Name")

== REQUIREMENTS:

* logging for slf4r/logging_logger

* log4r for slf4r/log4r_logger

== INSTALL:

* sudo gem install slf4r

* sudo gem install logging # optional

* sudo gem install log4r # optional

== LICENSE:

(The MIT License)

Copyright (c) 2009 kristian meier

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- ruby -*-

require 'rubygems'
require 'hoe'
require './lib/slf4r.rb'

Hoe.new('slf4r', Slf4r::VERSION) do |p|
# p.rubyforge_name = 'slf4rx' # if different than lowercase project name
p.developer('kristian', 'FIX@example.com')
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
end

desc 'Install the package as a gem.'
task :install => [:clean, :package] do
gem = Dir['pkg/*.gem'].first
sh "gem install --local #{gem} --no-ri --no-rdoc"
end

# vim: syntax=Ruby
12 changes: 12 additions & 0 deletions lib/log4r_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Log4r
class Logger

def initialize(_fullname, _level=nil, _additive=true, _trace=false)
@logger = ::Slf4r::LoggerFacade.new(_fullname)
end

def method_missing(method, *args, &block)
@logger.send(method, *args, &block) if @logger.respond_to?(method)
end
end
end
16 changes: 16 additions & 0 deletions lib/logging_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Logging
class Logger

def initialize(name)
@logger = ::Slf4r::LoggerFacade.new(name)
end

class << self
alias :[] :new
end

def method_missing(method, *args, &block)
@logger.send(method, *args, &block) if @logger.respond_to?(method)
end
end
end
10 changes: 10 additions & 0 deletions lib/ruby_logger_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Logger

def initialize(*args)
@logger = ::Slf4r::LoggerFacade.new(:root)
end

def method_missing(method, *args, &block)
@logger.send(method, *args, &block) if @logger.respond_to?(method)
end
end
8 changes: 8 additions & 0 deletions lib/slf4r.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'rubygems'
require 'pathname'

dir = Pathname(__FILE__).dirname.expand_path + 'slf4r'

require dir + 'version'
require dir + 'logger'

70 changes: 70 additions & 0 deletions lib/slf4r/abstract_logger_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module Slf4r
class AbstractLoggerFacade

protected

def format(exception)
": #{exception.message}:\n\t#{exception.backtrace.join('\n\t')}" if exception
end

def _debug(msg)
raise NotImplementedError
end

def _info(msg)
raise NotImplementedError
end

def _warn(msg)
raise NotImplementedError
end

def _error(msg)
raise NotImplementedError
end

public

attr_reader :name

def initialize(name)
@name = name
end

def debug?
raise NotImplementedError
end

def debug(msg = nil, exception = nil)
msg, exception = yield if block_given?
_debug("#{msg}#{format(exception)}")
end

def info?
raise NotImplementedError
end

def info(msg = nil, exception = nil)
msg, exception = yield if block_given?
_info("#{msg}#{format(exception)}")
end

def warn?
raise NotImplementedError
end

def warn(msg = nil, exception = nil)
msg, exception = yield if block_given?
_warn("#{msg}#{format(exception)}")
end

def error?
raise NotImplementedError
end

def error(msg = nil, exception = nil)
msg, exception = yield if block_given?
_error("#{msg}#{format(exception)}")
end
end
end
9 changes: 9 additions & 0 deletions lib/slf4r/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Slf4r
module Logger

def logger
@logger ||= LoggerFacade.new(self.class == Class ? name : self.class)
end

end
end
Loading

0 comments on commit 55e4742

Please sign in to comment.