pax_global_header00006660000000000000000000000064123554452040014516gustar00rootroot0000000000000052 comment=21be82ae567f9567cf211de280078d5ae5a04cdd logger-application-0.0.2/000077500000000000000000000000001235544520400152755ustar00rootroot00000000000000logger-application-0.0.2/.gitignore000066400000000000000000000002711235544520400172650ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp *.bundle *.so *.o *.a mkmf.log logger-application-0.0.2/.travis.yml000066400000000000000000000001311235544520400174010ustar00rootroot00000000000000language: ruby rvm: - 2.0.0 - 2.1 - ruby-head script: bundle exec rake test spec logger-application-0.0.2/BSDL000066400000000000000000000023721235544520400157500ustar00rootroot00000000000000Copyright (C) 2014 Hiroshi SHIBATA, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. logger-application-0.0.2/Gemfile000066400000000000000000000001471235544520400165720ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in logger-application.gemspec gemspec logger-application-0.0.2/LICENSE.txt000066400000000000000000000047101235544520400171220ustar00rootroot00000000000000Ruby is copyrighted free software by Yukihiro Matsumoto . You can redistribute it and/or modify it under either the terms of the 2-clause BSDL (see the file BSDL), or the conditions below: 1. You may make and give away verbatim copies of the source form of the software without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may modify your copy of the software in any way, provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or by allowing the author to include your modifications in the software. b) use the modified software only within your corporation or organization. c) give non-standard binaries non-standard names, with instructions on where to get the original software distribution. d) make other distribution arrangements with the author. 3. You may distribute the software in object code or binary form, provided that you do at least ONE of the following: a) distribute the binaries and library files of the software, together with instructions (in the manual page or equivalent) on where to get the original distribution. b) accompany the distribution with the machine-readable source of the software. c) give non-standard binaries non-standard names, with instructions on where to get the original software distribution. d) make other distribution arrangements with the author. 4. You may modify and include the part of the software into any other software (possibly commercial). But some files in the distribution are not written by the author, so that they are not under these terms. For the list of those files and their copying conditions, see the file LEGAL. 5. The scripts and library files supplied as input to or produced as output from the software do not automatically fall under the copyright of the software, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this software. 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. logger-application-0.0.2/README.md000066400000000000000000000024311235544520400165540ustar00rootroot00000000000000# Logger::Application [![Gem Version](https://badge.fury.io/rb/logger-application.png)](http://badge.fury.io/rb/logger-application) [![Build Status](https://secure.travis-ci.org/hsbt/logger-application.png)](http://travis-ci.org/hsbt/logger-application) ## Description Add logging support to your application. ## Installation Add this line to your application's Gemfile: gem 'logger-application' And then execute: $ bundle Or install it yourself as: $ gem install logger-application ## Usage 1. Define your application class as a sub-class of this class. 2. Override the +run+ method in your class to do many things. 3. Instantiate it and invoke #start. ## Example ```ruby class FooApp < Logger::Application def initialize(foo_app, application_specific, arguments) super('FooApp') # Name of the application. end def run ... log(WARN, 'warning', 'my_method1') ... @log.error('my_method2') { 'Error!' } ... end end status = FooApp.new(....).start ``` ## Contributing 1. Fork it ( https://github.com/hsbt/logger-application/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request logger-application-0.0.2/Rakefile000066400000000000000000000003771235544520400167510ustar00rootroot00000000000000require 'rake' require 'rake/testtask' require 'rspec/core/rake_task' require "bundler/gem_tasks" Rake::TestTask.new do |t| t.test_files = Dir.glob('test/**/test_*.rb') end RSpec::Core::RakeTask.new(:spec) do |t| t.pattern = "spec/**/*_spec.rb" end logger-application-0.0.2/lib/000077500000000000000000000000001235544520400160435ustar00rootroot00000000000000logger-application-0.0.2/lib/logger-application.rb000066400000000000000000000000351235544520400221460ustar00rootroot00000000000000require 'logger/application' logger-application-0.0.2/lib/logger/000077500000000000000000000000001235544520400173225ustar00rootroot00000000000000logger-application-0.0.2/lib/logger/application.rb000066400000000000000000000055721235544520400221630ustar00rootroot00000000000000require 'logger' require "logger/application/version" class Logger # # == Description # # Logger::Application --- Add logging support to your application. # # == Usage # # 1. Define your application class as a sub-class of this class. # 2. Override the +run+ method in your class to do many things. # 3. Instantiate it and invoke #start. # # == Example # # class FooApp < Logger::Application # def initialize(foo_app, application_specific, arguments) # super('FooApp') # Name of the application. # end # # def run # ... # log(WARN, 'warning', 'my_method1') # ... # @log.error('my_method2') { 'Error!' } # ... # end # end # # status = FooApp.new(....).start # class Application include Logger::Severity # Name of the application given at initialize. attr_reader :appname # # :call-seq: # Logger::Application.new(appname = '') # # == Args # # +appname+:: Name of the application. # # == Description # # Create an instance. Log device is +STDERR+ by default. This can be # changed with #set_log. # def initialize(appname = nil) @appname = appname @log = Logger.new(STDERR) @log.progname = @appname @level = @log.level end # # Start the application. Return the status code. # def start status = -1 begin log(INFO, "Start of #{ @appname }.") status = run rescue log(FATAL, "Detected an exception. Stopping ... #{$!} (#{$!.class})\n" << $@.join("\n")) ensure log(INFO, "End of #{ @appname }. (status: #{ status })") end status end # Logger for this application. See the class Logger for an explanation. def logger @log end # # Sets the logger for this application. See the class Logger for an # explanation. # def logger=(logger) @log = logger @log.progname = @appname @log.level = @level end # # Sets the log device for this application. See Logger.new for # an explanation of the arguments. # def set_log(logdev, shift_age = 0, shift_size = 1024000) @log = Logger.new(logdev, shift_age, shift_size) @log.progname = @appname @log.level = @level end def log=(logdev) set_log(logdev) end # # Set the logging threshold, just like Logger#level=. # def level=(level) @level = level @log.level = @level end # # See Logger#add. This application's +appname+ is used. # def log(severity, message = nil, &block) @log.add(severity, message, @appname, &block) if @log end private def run # TODO: should be an NotImplementedError raise RuntimeError.new('Method run must be defined in the derived class.') end end end logger-application-0.0.2/lib/logger/application/000077500000000000000000000000001235544520400216255ustar00rootroot00000000000000logger-application-0.0.2/lib/logger/application/version.rb000066400000000000000000000001011235544520400236270ustar00rootroot00000000000000class Logger class Application VERSION = "0.0.2" end end logger-application-0.0.2/logger-application.gemspec000066400000000000000000000020231235544520400224170ustar00rootroot00000000000000# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'logger/application/version' Gem::Specification.new do |spec| spec.name = "logger-application" spec.version = Logger::Application::VERSION spec.authors = ["SHIBATA Hiroshi"] spec.email = ["hsbt@ruby-lang.org"] spec.summary = %q{Add logging support to your application.} spec.description = %q{Add logging support to your application.} spec.homepage = "" spec.license = "2-clause BSDL" spec.files = `git ls-files -z`.split("\x0") spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.6" spec.add_development_dependency "rake" spec.add_development_dependency "test-unit" spec.add_development_dependency "rspec", "~> 2.14.1" spec.add_development_dependency "mspec" end logger-application-0.0.2/spec/000077500000000000000000000000001235544520400162275ustar00rootroot00000000000000logger-application-0.0.2/spec/level_spec.rb000066400000000000000000000020221235544520400206710ustar00rootroot00000000000000require 'spec_helper' describe "Logger::Application#level=" do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") @app = LoggerSpecs::TestApp.new("TestApp", @log_file) end after :each do @log_file.close unless @log_file.closed? rm_r @file_path end it "sets the logging threshold" do @app.level = Logger::ERROR @app.start @app.log(Logger::WARN, "Don't show me") @app.log(Logger::ERROR, "Show me") @log_file.rewind messages = @log_file.readlines messages.length.should == 1 LoggerSpecs::strip_date(messages.first).should == "ERROR -- TestApp: Show me\n" end it "can set the threshold to unknown values" do @app.level = 10 @app.start @app.log(Logger::INFO, "Info message") @app.log(Logger::DEBUG, "Debug message") @app.log(Logger::WARN, "Warn message") @app.log(Logger::ERROR, "Error message") @app.log(Logger::FATAL, "Fatal message") @log_file.rewind @log_file.readlines.should be_empty end end logger-application-0.0.2/spec/log_spec.rb000066400000000000000000000042311235544520400203470ustar00rootroot00000000000000require 'spec_helper' describe "Logger::Application#log" do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") @app = LoggerSpecs::TestApp.new("TestApp", @log_file) @app.start end after :each do @log_file.close unless @log_file.closed? rm_r @file_path end it "logs a message" do @app.log(Logger::WARN, "Test message") @log_file.rewind message = @log_file.readlines.last LoggerSpecs::strip_date(message).should == "WARN -- TestApp: Test message\n" end it "receives a severity" do @app.log(Logger::INFO, "Info message") @app.log(Logger::DEBUG, "Debug message") @app.log(Logger::WARN, "Warn message") @app.log(Logger::ERROR, "Error message") @app.log(Logger::FATAL, "Fatal message") @log_file.rewind messages = @log_file.readlines[3..-1] # remove default messages LoggerSpecs::strip_date(messages[0]).should == "INFO -- TestApp: Info message\n" LoggerSpecs::strip_date(messages[1]).should == "DEBUG -- TestApp: Debug message\n" LoggerSpecs::strip_date(messages[2]).should == "WARN -- TestApp: Warn message\n" LoggerSpecs::strip_date(messages[3]).should == "ERROR -- TestApp: Error message\n" LoggerSpecs::strip_date(messages[4]).should == "FATAL -- TestApp: Fatal message\n" end it "uses app name for Application Name" do @app.log(Logger::INFO, "Info message") @log_file.rewind test_message = @log_file.readlines.last Regexp.new(/TestApp/).should =~ LoggerSpecs::strip_date(test_message) end it "receives a block and calls it if message is nil" do temp = 0 @app.log(Logger::INFO, nil) { temp = 1 } temp.should == 1 end end describe "Logger::Application#log=" do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") @app = LoggerSpecs::TestApp.new("TestApp", @log_file) @app.start end after :all do if @log_file @log_file.close rm_r @file_path end end it "sets the log device" do regex = /STDERR Message/ @app.log = STDERR lambda { @app.log(Logger::WARN, "STDERR Message") }.should output_to_fd(regex, STDERR) end end logger-application-0.0.2/spec/new_spec.rb000066400000000000000000000025141235544520400203610ustar00rootroot00000000000000require 'spec_helper' describe "Logger::Application.new" do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") end after :each do @log_file.close unless @log_file.closed? rm_r @file_path end it "starts the logger on a new application" do LoggerSpecs::TestApp.new("TestApp", @log_file).start @log_file.rewind # go back to the beginning to read the contents first, second, third = @log_file.readlines LoggerSpecs::strip_date(first).should == "INFO -- TestApp: Start of TestApp.\n" LoggerSpecs::strip_date(second).should == "WARN -- TestApp: Test log message\n" LoggerSpecs::strip_date(third).should == "INFO -- TestApp: End of TestApp. (status: true)\n" end it "defaults application name to ''" do LoggerSpecs::TestApp.new(nil, @log_file).start @log_file.rewind first, second, third = @log_file.readlines LoggerSpecs::strip_date(first).should == "INFO -- : Start of .\n" LoggerSpecs::strip_date(second).should == "WARN -- : Test log message\n" LoggerSpecs::strip_date(third).should == "INFO -- : End of . (status: true)\n" end it "defaults logs to STDERR" do regex = /INFO.*WARN.*INFO.*/m lambda { LoggerSpecs::TestApp.new(nil, nil).start }.should output_to_fd(regex, STDERR) @log_file.rewind end end logger-application-0.0.2/spec/set_log_spec.rb000066400000000000000000000010051235544520400212160ustar00rootroot00000000000000require 'spec_helper' describe "Logger::Application#set_log"do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") @app = LoggerSpecs::TestApp.new("TestApp", @log_file) end after :each do @log_file.close unless @log_file.closed? rm_r @file_path end it "sets the log device for the logger" do regex = /STDERR Message/ @app.set_log(STDERR) lambda { @app.log(Logger::WARN, "STDERR Message") }.should output_to_fd(regex, STDERR) end end logger-application-0.0.2/spec/spec_helper.rb000066400000000000000000000007411235544520400210470ustar00rootroot00000000000000require 'logger/application' require 'mspec/matchers/output_to_fd' require 'tempfile' require 'fileutils' include FileUtils def tmp(path) Tempfile.create(path) end module LoggerSpecs def self.strip_date(str) str.gsub(/[A-Z].*\[.*\]/, "").lstrip end class TestApp < Logger::Application def initialize(appname, log_file=nil) super(appname) self.set_log(log_file) if log_file end def run log(WARN, "Test log message") end end end logger-application-0.0.2/spec/start_spec.rb000066400000000000000000000015461235544520400207310ustar00rootroot00000000000000require 'spec_helper' describe "Logger::Application#start" do before :each do @file_path = tmp("test_log.log") @log_file = File.open(@file_path, "w+") @app = LoggerSpecs::TestApp.new("TestApp", @log_file) end after :each do @log_file.close unless @log_file.closed? rm_r @file_path end it "starts the application logging start/end messages" do @app.start @log_file.rewind app_start, discard, app_end = @log_file.readlines LoggerSpecs::strip_date(app_start).should == "INFO -- TestApp: Start of TestApp.\n" LoggerSpecs::strip_date(app_end).should == "INFO -- TestApp: End of TestApp. (status: true)\n" end it "returns the status code" do code = @app.start @log_file.rewind app_end = @log_file.readlines.last /true/.should =~ LoggerSpecs::strip_date(app_end) code.should == true end end logger-application-0.0.2/test/000077500000000000000000000000001235544520400162545ustar00rootroot00000000000000logger-application-0.0.2/test/test_application.rb000066400000000000000000000022721235544520400221460ustar00rootroot00000000000000# coding: US-ASCII require 'test/unit' require 'logger/application' require 'tempfile' class TestLoggerApplication < Test::Unit::TestCase def setup @app = Logger::Application.new('appname') @tempfile = Tempfile.new("logger") @tempfile.close @filename = @tempfile.path File.unlink(@filename) end def teardown @tempfile.close(true) end def test_initialize app = Logger::Application.new('appname') assert_equal('appname', app.appname) end def test_start @app.set_log(@filename) begin @app.level = Logger::UNKNOWN @app.start # logs FATAL log assert_equal(1, File.read(@filename).split(/\n/).size) ensure @app.logger.close end end def test_logger @app.level = Logger::WARN @app.set_log(@filename) begin assert_equal(Logger::WARN, @app.logger.level) ensure @app.logger.close end @app.logger = logger = Logger.new(STDOUT) assert_equal(logger, @app.logger) assert_equal(Logger::WARN, @app.logger.level) @app.log = @filename begin assert(logger != @app.logger) assert_equal(Logger::WARN, @app.logger.level) ensure @app.logger.close end end end