Code Monkey home page Code Monkey logo

logger's Introduction

Logger

Logger is a simple but powerful logging utility to output messages in your Ruby program.

Logger has the following features:

  • Print messages to different levels such as info and error
  • Auto-rolling of log files
  • Setting the format of log messages
  • Specifying a program name in conjunction with the message

Installation

Add this line to your application's Gemfile:

gem 'logger'

And then execute:

$ bundle

Or install it yourself as:

$ gem install logger

Usage

Simple Example

require 'logger'

# Create a Logger that prints to STDOUT
log = Logger.new(STDOUT)
log.debug("Created Logger")

log.info("Program finished")

# Create a Logger that prints to STDERR
error_log = Logger.new(STDERR)
error_log = error_log.error("fatal error")

Development

After checking out the repo, run the following to install dependencies.

$ bin/setup

Then, run the tests as:

$ rake test

To install this gem onto your local machine, run

$ rake install

To release a new version, update the version number in lib/logger/version.rb, and then run

$ rake release

which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Advanced Development

Run tests of a specific file

$ ruby test/logger/test_logger.rb

Run tests filtering test methods by a name

--name option is available as:

$ ruby test/logger/test_logger.rb --name test_lshift

Publish documents to GitHub Pages

$ rake gh-pages

Then, git commit and push the generated HTMLs onto gh-pages branch.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/logger.

License

The gem is available as open source under the terms of the BSD-2-Clause.

logger's People

Contributors

akr avatar amatsuda avatar bazay avatar burdettelamar avatar colby-swandale avatar dependabot[bot] avatar drbrain avatar eregon avatar hsbt avatar ioquatix avatar jeremyevans avatar k0kubun avatar liijunwei avatar mame avatar marcandre avatar methodmissing avatar mperham avatar nagachika avatar nobu avatar nurse avatar olleolleolle avatar petergoldstein avatar pocke avatar rafaelfranca avatar segiddins avatar sonots avatar stomar avatar tenderlove avatar unak avatar znz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

logger's Issues

CI fails

with redefinition of Logger::VERSION

Pre-set formatter message inconsistency at UNKNOWN level

When using the formatter and the logger at a UNKNOWN level, the pre-set message string that is passed through to the proc is ANY instead of UNKNOWN

For all other logging levels the message is correct.

Couldn't find where this was generated in the source code, but advise either changing the message pre-set (Patch tweak), or altering the severity level (Major tweak)

EDIT: https://github.com/ruby/logger/blob/master/test/logger/test_logger.rb#L287 - This appears to be an error in the tests here, which looks like it's expecting the string "ANY"

Publishing docs

It's hard for anyone viewing this repository to find documentation about how to use the library. It would be good if we could publish the docs from rdoc, like what rake is doing: https://ruby.github.io/rake.

1.4.0 release on Rubygems is missing files

The lib/logger/ directory is missing in the 1.4.0 rubygems release. It’s present in the 1.4.0 GitHub release.

Traceback (most recent call last):
        9: from /rubygems/bin/nanoc:23:in `<main>'
        8: from /rubygems/bin/nanoc:23:in `load'
        7: from /rubygems/gems/nanoc-4.11.9/bin/nanoc:4:in `<top (required)>'
        6: from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require'
        5: from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require'
        4: from /rubygems/gems/nanoc-4.11.9/lib/nanoc.rb:42:in `<top (required)>'
        3: from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require'
        2: from /usr/share/rubygems/rubygems/core_ext/kernel_require.rb:54:in `require'
        1: from /rubygems/gems/logger-1.4.0/lib/logger.rb:15:in `<top (required)>'
/rubygems/gems/logger-1.4.0/lib/logger.rb:15:in `require_relative': cannot load such file -- /rubygems/gems/logger-1.4.0/lib/logger/version (LoadError)

Suggest adding `severity!` methods to change log level

It's a little bit inconvenient to write logger.level = Logger::DEBUG. It also means that the API can't be entirely replaced without reusing the values for logger.level exactly as defined by Logger. It would be nice if we could have methods for changing log level, e.g. logger.debug! which changes the log level to Logger::DEBUG and so on for other log levels.

Log rotation doesn't preserve file permissions

If you create a file with user permissions but then run logger under root the file rotate will not preserve these permissions causing access issues.

% ls -al log.log 
-rw-r--r--  1 reginaldsmith  staff  0 23 Sep 11:17 log.log
% rvmsudo irb
2.7.2 :001 > require "logger"
 => true 
2.7.2 :004 > puts File.stat("log.log").uid
502
 => nil 
2.7.2 :005 > log = File.open("log.log", "a")
 => #<File:log.log> 
2.7.2 :006 > logger = Logger.new(log, "everytime")
 => #<Logger:0x00007fd7cfbc5ee0 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x00007fd7cfbc5eb8 @datetime_format=nil>, @formatte... 
2.7.2 :007 > logger.info "hello"
 => true 
2.7.2 :008 > puts File.stat("log.log").uid
0 
2.7.2 :010 > exit
% ls -al log.log
-rw-r--r--  1 root  staff  123 23 Sep 11:20 log.log
% irb
2.7.2 :001 > require "logger"
 => true 
2.7.2 :002 > log = File.open("log.log", "a")
Errno::EACCES (Permission denied @ rb_sysopen - log.log)
>

Per-thread log level?

Hi, Sidekiq allows users to enable logging per-job so you can do this:

SomeJob.set(log_level: :debug).perform_async(123)

This requires a thread-local log level. I have a ::Logger monkeypatch to enable this feature. Would you like me to create a PR to upstream it? This would allow the user to do something like:

log = ::Logger.new(STDOUT, level: Logger::INFO)
log.debug("Not logged")

Thread.new do
  log.at_level(Logger::DEBUG) do
    log.debug("Logged")
  end
end.join

log.debug("Not logged")

https://github.com/mperham/sidekiq/blob/c12c8b567fcf3357bbd8b82a7d8651ddd7cc30a3/lib/sidekiq/logger.rb#L25-L73

Testing seems broken

x_x > ruby test/logger/test_logger.rb --location 40
Traceback (most recent call last):
	9: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:1073:in `block in autorun'
	8: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:39:in `run_once'
	7: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:1074:in `block (2 levels) in autorun'
	6: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:33:in `run'
	5: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:616:in `run'
	4: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:786:in `run'
	3: from /Users/samuel/Documents/ruby/logger/test/lib/minitest/unit.rb:1084:in `run'
	2: from /Users/samuel/Documents/ruby/logger/test/lib/minitest/unit.rb:1091:in `_run'
	1: from /Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:130:in `process_args'
/Users/samuel/Documents/ruby/logger/test/lib/test/unit.rb:61:in `process_args': invalid option: --location (OptionParser::InvalidOption)

Does not accept `:daily` as shift_age

Logger.new fails if shift_age is a symbol.

Error:

irb(main):003:0> Logger.new('a.log', :daily)
ArgumentError (invalid :shift_age :daily, should be daily, weekly, monthly, or everytime)

Environment:

$ bundle list
Gems included by the bundle:
  * bundler (2.0.2)
  * logger (1.4.2)

Expected behavior (My suggestion for design change):

Logger.new('a.log', :daily) works as well as Logger.new('a.log', 'daily').

Color

I can create this file:

require 'logger'
z = Logger.new STDOUT
z.info 'abcde'
z.warn 'abcde'
z.error 'abcde'

but it outputs in black and white. Is color an option?

Broken link

lib/logger.rb line 269 has broken link:

{level}[Logger.html#attribute-i-level]:

Aws Lambdas - undefined method `[]' for nil:NilClass

Getting some errors with the logger gem when executing

logger = Logger.new($stdout)
logger.info 'test'
  "errorMessage": "undefined method `[]' for nil:NilClass",
  "errorType": "Function<NoMethodError>",
  "stackTrace": [
    "/var/task/vendor/bundle/ruby/3.2.0/gems/logger-1.6.0/lib/logger.rb:384:in `level'",
    "/var/task/vendor/bundle/ruby/3.2.0/gems/logger-1.6.0/lib/logger.rb:653:in `add'",
    "/var/task/vendor/bundle/ruby/3.2.0/gems/logger-1.6.0/lib/logger.rb:697:in `info'",

error does not appear when using version 1.5.3 of the gem

v1.5.3...v1.6.0

For some reason it looks like the initialize function is not being triggered and thus @level_override is never set to {}

Update changelog?

There are undocumented versions not in the changelog. If you want a native English speaker, I would be glad to volunteer.

Log rotation inter-process lock failed, Issue occurs in JRuby 9.3.0.0 and Windows

When Using JRuby 9.3.0.0 in Windows this error occurs.
Reproduction steps:

1) Test script:
`test_logger.rb

logger = Logger.new("D:/log.txt", 5, 1024)
  
150.times do |i|
  logger.info i
end

2) run script:
The easiest way to run JRuby in windows would be installing Java 11 and downloading jruby-complete-9.3.0.0.jar, then run:

D:\>java -jar jruby-complete-9.3.0.0.jar test_logger.rb

3) Console output:
log rotation inter-process lock failed. D:\log.txt -> D:\log.txt.0: The process cannot access the file because it is being used by another process.
log writing failed. closed stream
log shifting failed. closed stream
log writing failed. closed stream
log shifting failed. closed stream
...

4) Cause:
The issue occurs due JRuby returns java for RUBY_PLATFORM when loading the method lock_shift_log

Default shift values not available to logfiles set from #reopen

This code correctly sets @shift_age to 7 and @shift_size to 1048576

Logger::LogDevice.new(@filename)

This code will end up with those instance variables set to nil

logger = Logger::LogDevice.new(STDERR)
logger.reopen(@filename)

This could be fixed in LogDevice#initialize by setting the defaults outside of the if @filename condition, or repeating the logic in the LogDevice#reopen.

It would also make sense, though, if you can update the output to be able to also change those values if desired. This could be done by adding an attr_writer for them.

The best course of action would be to support adding/changing the values when a new output is specified. I'm adding a PR with my recommendation.

My specific use case involves a library I'm using instantiating a logger instance for me with STDOUT, and I want to change the output to a file with useful shift values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.