Git Product home page Git Product logo

journald-logger's Introduction

journald-logger

A Logger drop-in replacement that logs directly to systemd-journal with some additional features

Usage

require 'journald/logger'

logger = Journald::Logger.new('gandalf') # simple logger, sets SYSLOG_IDENTIFIER to 'gandalf'
logger = Journald::TraceLogger.new('gandalf') # tracing logger, logs caller's file, line number and function name

Logger replacement

This gem is designed to be accurate drop-in Logger replacement

logger.warn "you shall not pass!"           # works
logger.info("gollum") { "my preciousss" }   # also works!
logger.progname = "saruman"                 # sets value for SYSLOG_IDENTIFIER to 'saruman'
logger.formatter = SomeFormatter.new        # does nothing, journald-logger does not require a formatter
logger.close                                # does nothing, nothing to close

We map Ruby severity to Syslog priority by this map

LEVEL_MAP = {
  ::Logger::UNKNOWN => LOG_ALERT,
  ::Logger::FATAL   => LOG_CRIT,
  ::Logger::ERROR   => LOG_ERR,
  ::Logger::WARN    => LOG_WARNING,
  ::Logger::INFO    => LOG_INFO,
  ::Logger::DEBUG   => LOG_DEBUG,
}

You may notice it's somewhat different from the one from Syslog::Logger

Setting report level

logger = Journald::Logger.new('gandalf', Journald::LOG_NOTICE) # set minimal reporting level to notice
logger.min_priority   = Journald::LOG_NOTICE # runtime change of minimal priority
logger.level          = Logger::WARN # use Logger severity
logger.sev_threshold  = Logger::INFO # please pay attention that Logger severity lacks several levels e.g. 'notice'

Tags

Tags are used to add systemd-journal fields to all subsequent log calls until removed

logger = Journald::Logger.new('gandalf', world: 'arda') # set world tag in costructor
logger.tag location: 'shire', weapon: 'staff' # add/replace location and weapon
logger.tag(location: 'moria', object: 'balrog') do # change location and use object in the block
  # log as 'MESSAGE=you shall not pass!', 'PRIORITY=4', 'LOCATION=moria', 'OBJECT=balrog', 'WORLD=arda', 'WEAPON=staff'
  logger.warn 'you shall not pass!'
end # return location & object to the previous state
# log as 'MESSAGE=That was not in canon!', 'PRIORITY=6', 'LOCATION=shire', 'WORLD=arda', 'WEAPON=staff'
logger.info 'That was not in canon!'
logger.untag :location, :weapon # remove location and weapon

Tag names must follow systemd-journal fields naming convention: letters, numbers, underscores, cannot begin with underscore. Library upcases all letters automatically

systemd-journal style

Two methods which look similarly to native systemd-journal api

logger.send_message(
  message: 'hi!',
  priority: Journald::LOG_NOTICE,
  any_field: 'any_value',
) # tags will be added here
logger.print Journald::LOG_NOTICE, 'hi!' # and here

Syslog style

Just to add some more confusion you may use methods with syslog severity names prefixed with log_

logger.log_err 'Error'
logger.log_debug 'Debug'

Exception logging

begin
  raise "Aw, snap!"
rescue => e
  logger.exception e # log exception with LOG_ERR level by default
  logger.exception e, severity: Logger::WARN        # use Logger severity
  logger.exception e, priority: Journald::LOG_ALERT # use Syslog priority 
end

Exception logger automatically fills the following fields:

EXCEPTION_CLASS=ExceptionRealClassName
EXCEPTION_MESSAGE=Original exception message
BACKTRACE=full backtrace
CAUSE=exception cause (Ruby >= 2.1)
GEM_LOGGER_MESSAGE_TYPE=Exception

In Ruby 2.1+ it also tries to log CODE_LINE, CODE_FILE and CODE_FUNC and to recurse into Cause and log it into a separate message with GEM_LOGGER_MESSAGE_TYPE=ExceptionCause

Authors

This library was written by Anton Smirnov and currently maintained by https://www.theforeman.org developers.

License

MIT, see LICENSE.txt

journald-logger's People

Contributors

alexandermeindl avatar arokettu avatar lzap avatar mmoll avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

lzap arokettu

journald-logger's Issues

send method collides with ruby's builtin Object.send

I've hit an issue where the journald-logger gem does not work as a drop-in replacement for Logger.

The delayed_job code calls the logger by running

logger.send(level, "#{Time.now.strftime('%FT%T%z')}: #{text}")

where level is a symbol, like :info, :debug etc. It expects to be calling the ruby Object.send method, which will forward to the appropriate instance method logger.info, logger.debug, etc. depending on the log level.

However, journald-logger overrides the send method with a method that behaves completely differently, so this doesn't work.

(Note that the code using the send method can be changed to call __send__ instead to work around this issue, but requiring code changes means it's not a drop-in replacement.)

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.