la_logger 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +3 -2
- data/benchmark/singleton.rb +5 -4
- data/la_logger.gemspec +3 -3
- data/lib/la_logger/version.rb +1 -1
- data/lib/la_logger.rb +37 -8
- data/spec/helpers/mongo_helper.rb +4 -3
- data/spec/la_logger_spec.rb +31 -5
- metadata +20 -6
- data/spec/helpers/io_helper.rb +0 -30
    
        data/README.md
    CHANGED
    
    | @@ -4,12 +4,13 @@ Require: ruby 1.9.x, fluent-logger (not working on JRuby without modification). | |
| 4 4 |  | 
| 5 5 | 
             
            ## Concepts
         | 
| 6 6 |  | 
| 7 | 
            -
            - Initialize a logger object with ` | 
| 7 | 
            +
            - Initialize a logger object with `tag` (which becomings collection name for `tag_mapped`
         | 
| 8 | 
            +
              setting in `fluent.conf`) and log level:
         | 
| 8 9 |  | 
| 9 10 | 
             
                class LoggerUser
         | 
| 10 11 | 
             
                  def logger
         | 
| 11 12 | 
             
                    # initialize tag and log level, default level is 'WARN'
         | 
| 12 | 
            -
                    LaLogger.new(' | 
| 13 | 
            +
                    LaLogger.new('specific_tag', LaLogger::ERROR)
         | 
| 13 14 | 
             
                  end
         | 
| 14 15 |  | 
| 15 16 | 
             
                  def logger_this
         | 
    
        data/benchmark/singleton.rb
    CHANGED
    
    | @@ -1,3 +1,4 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 1 2 | 
             
            require 'benchmark'
         | 
| 2 3 |  | 
| 3 4 | 
             
            require 'fluent-logger'
         | 
| @@ -11,17 +12,17 @@ Benchmark.bm do |x| | |
| 11 12 | 
             
              x.report('FluentLogger.new') do
         | 
| 12 13 | 
             
                n.times do
         | 
| 13 14 | 
             
                  Fluent::Logger::FluentLogger.new(
         | 
| 14 | 
            -
                    nil, host: 'localhost', port:  | 
| 15 | 
            -
                  ).post('la.logger1', {error: 'test message'})
         | 
| 15 | 
            +
                    nil, host: 'localhost', port: 24_224
         | 
| 16 | 
            +
                  ).post('la.logger1', { error: 'test message' })
         | 
| 16 17 | 
             
                end
         | 
| 17 18 | 
             
              end
         | 
| 18 19 |  | 
| 19 20 | 
             
              x.report('FluentLogger preload') do
         | 
| 20 21 | 
             
                logger = Fluent::Logger::FluentLogger.new(
         | 
| 21 | 
            -
                    nil, host: 'localhost', port:  | 
| 22 | 
            +
                    nil, host: 'localhost', port: 24_224
         | 
| 22 23 | 
             
                  )
         | 
| 23 24 | 
             
                n.times do
         | 
| 24 | 
            -
                  logger.post('la.logger1', {error: 'test message'})
         | 
| 25 | 
            +
                  logger.post('la.logger1', { error: 'test message' })
         | 
| 25 26 | 
             
                end
         | 
| 26 27 | 
             
              end
         | 
| 27 28 |  | 
    
        data/la_logger.gemspec
    CHANGED
    
    | @@ -2,8 +2,8 @@ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__) | |
| 2 2 | 
             
            require 'la_logger/version'
         | 
| 3 3 |  | 
| 4 4 | 
             
            Gem::Specification.new 'la_logger', LaLogger::VERSION do |s|
         | 
| 5 | 
            -
              s.description       = 'A singleton wrapper for fluent logger | 
| 6 | 
            -
              s.summary           = 'Wrap fluent logger as a singleton object, silently fail-over to  | 
| 5 | 
            +
              s.description       = 'A singleton wrapper for fluent logger.'
         | 
| 6 | 
            +
              s.summary           = 'Wrap fluent logger as a singleton object, silently fail-over to STDERR if fluentd service not defined.'
         | 
| 7 7 | 
             
              s.authors           = ['Huang Wei']
         | 
| 8 8 | 
             
              s.email             = 'huangw@7lime.com'
         | 
| 9 9 | 
             
              s.homepage          = 'https://github.com/7lime/la_logger-gem'
         | 
| @@ -12,9 +12,9 @@ Gem::Specification.new 'la_logger', LaLogger::VERSION do |s| | |
| 12 12 | 
             
              s.test_files        = Dir.glob('{spec,test}/**/*.rb')
         | 
| 13 13 |  | 
| 14 14 | 
             
              s.add_dependency 'fluent-logger'
         | 
| 15 | 
            +
              s.add_dependency 'activesupport', '~> 3.2'
         | 
| 15 16 | 
             
              s.add_development_dependency 'rspec', '~> 2.5'
         | 
| 16 17 | 
             
              s.add_development_dependency 'mongo'
         | 
| 17 18 | 
             
              s.add_development_dependency 'bson_ext'
         | 
| 18 19 | 
             
              s.add_development_dependency 'simplecov', '~> 2.5'
         | 
| 19 20 | 
             
            end
         | 
| 20 | 
            -
             | 
    
        data/lib/la_logger/version.rb
    CHANGED
    
    
    
        data/lib/la_logger.rb
    CHANGED
    
    | @@ -4,6 +4,8 @@ require 'forwardable' | |
| 4 4 | 
             
            require 'singleton'
         | 
| 5 5 | 
             
            require 'fluent-logger'
         | 
| 6 6 | 
             
            require 'logger'
         | 
| 7 | 
            +
            require 'active_support/backtrace_cleaner'
         | 
| 8 | 
            +
            require 'active_support/inflector' # :underscore
         | 
| 7 9 |  | 
| 8 10 | 
             
            # LaLogger: wraper for fluent-logger
         | 
| 9 11 | 
             
            class LaLogger
         | 
| @@ -20,8 +22,8 @@ class LaLogger | |
| 20 22 | 
             
                def initialize
         | 
| 21 23 | 
             
                  if ENV['FLUENTD']
         | 
| 22 24 | 
             
                    host_, port_, tag_ = ENV['FLUENTD'].split ':'
         | 
| 23 | 
            -
                    host_ = 'localhost' unless host_  | 
| 24 | 
            -
                    port_ =  | 
| 25 | 
            +
                    host_ = 'localhost' unless host_ && host_.size > 0
         | 
| 26 | 
            +
                    port_ = 24_224 unless port_ && port_.size > 0
         | 
| 25 27 | 
             
                    @logger = Fluent::Logger::FluentLogger.new(
         | 
| 26 28 | 
             
                      tag_, host: host_.to_s, port: port_.to_i
         | 
| 27 29 | 
             
                    )
         | 
| @@ -43,15 +45,42 @@ class LaLogger | |
| 43 45 | 
             
              end
         | 
| 44 46 |  | 
| 45 47 | 
             
              # define 6 log methods
         | 
| 46 | 
            -
              [:debug, :info, :warn, | 
| 47 | 
            -
             | 
| 48 | 
            -
                define_method(met) do |message,  | 
| 49 | 
            -
                  data[ | 
| 50 | 
            -
                  tag  | 
| 51 | 
            -
                  logger.post(tag, data) if @level <= lvl
         | 
| 48 | 
            +
              [:debug, :info, :warn,
         | 
| 49 | 
            +
               :error, :fatal, :unknown].each_with_index do |met, lvl|
         | 
| 50 | 
            +
                define_method(met) do |message, data = {}|
         | 
| 51 | 
            +
                  tag = data['tag'] || @tag
         | 
| 52 | 
            +
                  logger.post(tag, get_data(met, message, data)) if @level <= lvl
         | 
| 52 53 | 
             
                end
         | 
| 53 54 | 
             
              end
         | 
| 54 55 |  | 
| 55 56 | 
             
              # use LaLogger.new.post() directly.
         | 
| 56 57 | 
             
              def_delegator :logger, :post
         | 
| 58 | 
            +
             | 
| 59 | 
            +
              private
         | 
| 60 | 
            +
             | 
| 61 | 
            +
              # initialize a default error message back trace cleaner
         | 
| 62 | 
            +
              def bc
         | 
| 63 | 
            +
                unless @bc
         | 
| 64 | 
            +
                  @bc = ActiveSupport::BacktraceCleaner.new
         | 
| 65 | 
            +
                  if ENV['APP_ROOT']
         | 
| 66 | 
            +
                    @bc.add_filter   { |line| line.gsub(ENV['APP_ROOT'], '') }
         | 
| 67 | 
            +
                  end
         | 
| 68 | 
            +
                  @bc.add_silencer { |line| line =~ /ruby|gems/ }
         | 
| 69 | 
            +
                end
         | 
| 70 | 
            +
                @bc
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
              # set up data hash to feed the fluent-logger object
         | 
| 74 | 
            +
              def get_data(met, message, data)
         | 
| 75 | 
            +
                # message also can be omitted:
         | 
| 76 | 
            +
                data = message if message.is_a?(Hash) && data.keys.size == 0
         | 
| 77 | 
            +
                data[met] = message if message.is_a?(String)
         | 
| 78 | 
            +
                data[met] = message.class.name.underscore if message.is_a?(Exception)
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                # handle exception object
         | 
| 81 | 
            +
                data['message'] ||= message.message if message.respond_to?(:message)
         | 
| 82 | 
            +
                data['backtrace'] ||=
         | 
| 83 | 
            +
                  bc.clean(message.backtrace) if message.respond_to?(:backtrace)
         | 
| 84 | 
            +
                data
         | 
| 85 | 
            +
              end
         | 
| 57 86 | 
             
            end
         | 
| @@ -1,3 +1,4 @@ | |
| 1 | 
            +
            # encoding: utf-8
         | 
| 1 2 | 
             
            require 'mongo'
         | 
| 2 3 | 
             
            include Mongo
         | 
| 3 4 |  | 
| @@ -5,11 +6,11 @@ include Mongo | |
| 5 6 |  | 
| 6 7 | 
             
            module MongoHelper
         | 
| 7 8 | 
             
              def db
         | 
| 8 | 
            -
                MongoClient.new('localhost',  | 
| 9 | 
            +
                MongoClient.new('localhost', 27_017).db('lalogger_test')
         | 
| 9 10 | 
             
              end
         | 
| 10 11 |  | 
| 11 | 
            -
              def get_last_logs(col)
         | 
| 12 | 
            -
                db.collection(col).find({}, { limit:  | 
| 12 | 
            +
              def get_last_logs(col, size = 5)
         | 
| 13 | 
            +
                db.collection(col).find({}, { limit: size }).sort(time: :desc).to_a
         | 
| 13 14 | 
             
              end
         | 
| 14 15 |  | 
| 15 16 | 
             
              def cleanup
         | 
    
        data/spec/la_logger_spec.rb
    CHANGED
    
    | @@ -1,11 +1,7 @@ | |
| 1 1 | 
             
            # encoding: utf-8
         | 
| 2 | 
            -
            ENV['FLUENTD'] = 'localhost'
         | 
| 3 2 | 
             
            require 'spec_helper'
         | 
| 4 3 | 
             
            require 'la_logger'
         | 
| 5 4 |  | 
| 6 | 
            -
            #require 'helpers/io_helper'
         | 
| 7 | 
            -
            #include IOHelper
         | 
| 8 | 
            -
             | 
| 9 5 | 
             
            require 'helpers/mongo_helper'
         | 
| 10 6 | 
             
            include MongoHelper
         | 
| 11 7 |  | 
| @@ -39,6 +35,13 @@ class UserB | |
| 39 35 | 
             
              end
         | 
| 40 36 | 
             
            end
         | 
| 41 37 |  | 
| 38 | 
            +
            # mix-in la-logger, expose private methods
         | 
| 39 | 
            +
            class LaLogger
         | 
| 40 | 
            +
              def expose_data(message, data = {})
         | 
| 41 | 
            +
                get_data('error', message, data)
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
| 44 | 
            +
             | 
| 42 45 | 
             
            describe LaLogger do
         | 
| 43 46 | 
             
              describe 'singleton' do
         | 
| 44 47 | 
             
                it 'same logger globally' do
         | 
| @@ -48,6 +51,29 @@ describe LaLogger do | |
| 48 51 | 
             
                end
         | 
| 49 52 | 
             
              end
         | 
| 50 53 |  | 
| 54 | 
            +
              describe 'adjust message data' do
         | 
| 55 | 
            +
                it 'can omitt message' do
         | 
| 56 | 
            +
                  data = LaLogger.new.expose_data('message' => 'info')
         | 
| 57 | 
            +
                  data['message'].should eq('info')
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                it 'handles error object' do
         | 
| 61 | 
            +
                  class SomeExceptionClass < RuntimeError; end
         | 
| 62 | 
            +
                  begin
         | 
| 63 | 
            +
                    fail SomeExceptionClass, 'raise me!'
         | 
| 64 | 
            +
                  rescue SomeExceptionClass => e
         | 
| 65 | 
            +
                    data = LaLogger.new.expose_data(e)
         | 
| 66 | 
            +
                    data['error'].should eq('some_exception_class')
         | 
| 67 | 
            +
                    data['message'].should eq('raise me!')
         | 
| 68 | 
            +
                  end
         | 
| 69 | 
            +
                end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                it 'handle string message' do
         | 
| 72 | 
            +
                  data = LaLogger.new.expose_data('send out message')
         | 
| 73 | 
            +
                  data['error'].should eq('send out message')
         | 
| 74 | 
            +
                end
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
             | 
| 51 77 | 
             
              describe 'default level' do
         | 
| 52 78 | 
             
                it 'log error messages' do
         | 
| 53 79 | 
             
                  LaLogger.new('global').error('error message from global')
         | 
| @@ -71,5 +97,5 @@ describe LaLogger do | |
| 71 97 | 
             
                end
         | 
| 72 98 | 
             
              end
         | 
| 73 99 |  | 
| 74 | 
            -
              after(: | 
| 100 | 
            +
              after(:each) { cleanup }
         | 
| 75 101 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: la_logger
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0 | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013-09- | 
| 12 | 
            +
            date: 2013-09-25 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: fluent-logger
         | 
| @@ -27,6 +27,22 @@ dependencies: | |
| 27 27 | 
             
                - - ! '>='
         | 
| 28 28 | 
             
                  - !ruby/object:Gem::Version
         | 
| 29 29 | 
             
                    version: '0'
         | 
| 30 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 31 | 
            +
              name: activesupport
         | 
| 32 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 33 | 
            +
                none: false
         | 
| 34 | 
            +
                requirements:
         | 
| 35 | 
            +
                - - ~>
         | 
| 36 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 37 | 
            +
                    version: '3.2'
         | 
| 38 | 
            +
              type: :runtime
         | 
| 39 | 
            +
              prerelease: false
         | 
| 40 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 41 | 
            +
                none: false
         | 
| 42 | 
            +
                requirements:
         | 
| 43 | 
            +
                - - ~>
         | 
| 44 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 45 | 
            +
                    version: '3.2'
         | 
| 30 46 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 31 47 | 
             
              name: rspec
         | 
| 32 48 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -91,7 +107,7 @@ dependencies: | |
| 91 107 | 
             
                - - ~>
         | 
| 92 108 | 
             
                  - !ruby/object:Gem::Version
         | 
| 93 109 | 
             
                    version: '2.5'
         | 
| 94 | 
            -
            description: A singleton wrapper for fluent logger | 
| 110 | 
            +
            description: A singleton wrapper for fluent logger.
         | 
| 95 111 | 
             
            email: huangw@7lime.com
         | 
| 96 112 | 
             
            executables: []
         | 
| 97 113 | 
             
            extensions: []
         | 
| @@ -107,7 +123,6 @@ files: | |
| 107 123 | 
             
            - la_logger.gemspec
         | 
| 108 124 | 
             
            - lib/la_logger.rb
         | 
| 109 125 | 
             
            - lib/la_logger/version.rb
         | 
| 110 | 
            -
            - spec/helpers/io_helper.rb
         | 
| 111 126 | 
             
            - spec/helpers/mongo_helper.rb
         | 
| 112 127 | 
             
            - spec/la_logger_spec.rb
         | 
| 113 128 | 
             
            - spec/spec_helper.rb
         | 
| @@ -135,10 +150,9 @@ rubyforge_project: | |
| 135 150 | 
             
            rubygems_version: 1.8.23
         | 
| 136 151 | 
             
            signing_key: 
         | 
| 137 152 | 
             
            specification_version: 3
         | 
| 138 | 
            -
            summary: Wrap fluent logger as a singleton object, silently fail-over to  | 
| 153 | 
            +
            summary: Wrap fluent logger as a singleton object, silently fail-over to STDERR if
         | 
| 139 154 | 
             
              fluentd service not defined.
         | 
| 140 155 | 
             
            test_files:
         | 
| 141 | 
            -
            - spec/helpers/io_helper.rb
         | 
| 142 156 | 
             
            - spec/helpers/mongo_helper.rb
         | 
| 143 157 | 
             
            - spec/la_logger_spec.rb
         | 
| 144 158 | 
             
            - spec/spec_helper.rb
         | 
    
        data/spec/helpers/io_helper.rb
    DELETED
    
    | @@ -1,30 +0,0 @@ | |
| 1 | 
            -
            # encoding: utf-8
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            # record stdout and stderr output as strings for testing against expectation
         | 
| 4 | 
            -
            module IOHelper
         | 
| 5 | 
            -
              def capture_stdout(&block)
         | 
| 6 | 
            -
                original_stdout = $stdout
         | 
| 7 | 
            -
                $stdout = fake = StringIO.new
         | 
| 8 | 
            -
                begin
         | 
| 9 | 
            -
                  yield
         | 
| 10 | 
            -
                rescue SystemExit
         | 
| 11 | 
            -
                  $stdout = original_stdout # fake rubocop
         | 
| 12 | 
            -
                ensure
         | 
| 13 | 
            -
                  $stdout = original_stdout
         | 
| 14 | 
            -
                end
         | 
| 15 | 
            -
                fake.string
         | 
| 16 | 
            -
              end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
              def capture_stderr(&block)
         | 
| 19 | 
            -
                original_stdout = $stderr
         | 
| 20 | 
            -
                $stderr = fake = StringIO.new
         | 
| 21 | 
            -
                begin
         | 
| 22 | 
            -
                  yield
         | 
| 23 | 
            -
                rescue SystemExit
         | 
| 24 | 
            -
                  $stdout = original_stdout # fake rubocop
         | 
| 25 | 
            -
                ensure
         | 
| 26 | 
            -
                  $stderr = original_stdout
         | 
| 27 | 
            -
                end
         | 
| 28 | 
            -
                fake.string
         | 
| 29 | 
            -
              end
         | 
| 30 | 
            -
            end
         |