RubySmart::SimpleLogger



A simple, multifunctional logging library for Ruby (and Rails).
It features a fast, customizable logging with multi-device support (e.g. log to STDOUT AND file).
Special (PRE-defined) scenes can be used for interactive CLI and better logging visibility.
Installation
Add this line to your application's Gemfile:
gem 'ruby_smart-simple_logger'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install ruby_smart-simple_logger
Enhancements
- PRE-defined scenes to fastly create a simple, structured CLI output. (see Scenes)
- Better log-visibility with masked output through scenes
awesome_print
gem compatibility for a prettified object debug- Multi-device support (write to logfile & to STDOUT & to ... & to ...)
- 'klass_logger' instances for easier access (see klass_logger)
Examples
require 'simple_logger'
logger = ::SimpleLogger.new
logger.debug @my_custom_object, "test title"
logger = ::SimpleLogger.new(:memory)
logger.debug "some debug"
logger.info "some info"
logger.success "success called"
logger.logs
require 'simple_logger'
logger = ::SimpleLogger.new
logger.theme "Jobs to do"
logger.job_result "A custom job", true
logger.job_result "A other job", "failed", false
logger.job_result "Just a job", "unknown", :yellow
Usage
You can either create your own logger instance, by calling new on the ::SimpleLogger class or using the logger as a klass_logger.
Instance Usage
require 'simple_logger'
logger = ::SimpleLogger.new
logger.debug "some debug"
logger.error "that failed ..."
alternative_logger = ::SimpleLogger.new :memory
alternative_logger.debug "some debug, just in memory logs"
alternative_logger.error "that failed, also in memory logs..."
alternative_logger.logs
alternative_logger.logs_to_h
multi_logger = ::SimpleLogger.new :memory, :stdout, clr: true, format: :default
multi_logger.debug "some debug, sent to stdout & memory"
multi_logger.logs
You can also create a new instance from every klass_logger Object (see below).
klass_logger Usage
Instead of creating a new instance you can also create klass_logger on every module by simply extending the ::SimpleLogger::KlassLogger
module.
module MyCustomLogger
extend ::RubySmart::SimpleLogger::KlassLogger
self.klass_logger_opts = {builtin: :stdout, clr: true}
end
MyCustomLogger.debug "some debug"
MyCustomLogger.error "that failed ...", "It's an error title for better visibility"
MyCustomLogger.theme "My Theme"
MyCustomLogger.klass_logger_opts = {builtin: MyApp::Tasks::SpecialTask}
MyCustomLogger.info "Very nice here"
This is already done for the SimpleLogger
(or Debugger
) module - so you can directly access the methods:
require 'simple_logger'
SimpleLogger.debug "some debug"
SimpleLogger.error "that failed ..."
SimpleLogger.klass_logger_opts = {builtin: :memory, stdout: false}
SimpleLogger.debug "some other debug in memory only ..."
SimpleLogger.logs
other_logger = SimpleLogger.new
other_logger.debug "some other debug in memory only ..."
other_logger2 = SimpleLogger.new :rails, :stdout
other_logger2.info "directly logs to the rails logger"
other_logger2.info "and also to STDOUT"
Alternatives with already build 'Debugger':
require 'ruby_smart-debugger'
Debugger.debug "some debug"
Debugger.error "that failed ..."
Builtins
While you can just build a new logger (or use the klass_logger) without any arguments, you can also create a new one with builtins.
nil Builtin
A nil
builtin will auto-detect the best logging solution for you:
- For CLI or windowed programs it'll just send the logs to
STDOUT
. - For Debugging (e.g. IDE-related debugging gems) it'll send the logs to the
Debugger
. - For rails-applications it'll send to the current
Rails.logger
instance. - Otherwise it'll store logs temporary in memory (accessible through the #logs method)
Example:
logger = ::SimpleLogger.new
logger.debug "some debug"
stdout / stderr Builtin
A :stdout / :stderr
builtin will send to STDOUT / STDERR
and uses a colored output by default.
Example:
logger = ::SimpleLogger.new(:stdout)
logger.debug "some debug"
debugger Builtin
A :debugger
builtin will send to the Debugger
(e.g. your IDE's debugging gem)
Example:
logger = ::SimpleLogger.new(:debugger)
logger.debug "some debug"
null Builtin
A :null
builtin will silently swallow all logging data (so it will not be send).
Example:
logger = ::SimpleLogger.new(:null)
logger.debug "some debug"
rails Builtin
A :rails
builtin will always send to the Rails.logger
instance.
Example:
logger = ::SimpleLogger.new(:rails)
logger.debug "some debug"
proc Builtin
A :proc
builtin will call the provided proc (through `options[:proc]```) everytime a log will be written.
The data will be provided as array ( [severity, time, progname, data]
).
Example:
proc = lambda{|data| puts "---> #{data[0]} | #{data[3]} <---"}
logger = ::SimpleLogger.new(:proc, proc: proc)
logger.debug "some debug"
memory Builtin
A :memory
builtin will always store the logged data within an instance variable and can be accessed through the #logs
or #logs_to_h
methods.
Example:
logger = ::SimpleLogger.new(:memory)
logger.debug "some debug"
logger.info "some info"
logger.success "success called"
logger.logs
String Builtin
Providing a String
will always create and write to a new logfile.
Example:
logger = ::SimpleLogger.new('a_custom/logfile.log')
logger.debug "some debug"
other_logger = ::SimpleLogger.new('a_custom/other_logfile.log', clr: false, inspect: false)
other_logger.debug "some debug without color, but with mask"
noformat_logger = ::SimpleLogger.new('a_custom/noformat_logfile.log', format: :default, payload: false)
noformat_logger.debug "some debug without color and mask - uses the default format"
Module Builtin
Providing a module
will also create and write to a new logfile.
The path depends on the provided module name.
Example:
logger = ::SimpleLogger.new(Users::Jobs::Import)
logger.debug "some debug"
other Builtin
Providing any other Object must respond to `#write```.
Formats
The default formatter (if no other was provided through `opts[:formatter```) will provide the following PRE-defined formats:
Also prints a colored output by default.
default Format
The default format equals the Ruby's Formatter - by default also prints a colored output:
logger = ::SimpleLogger.new(:stdout, format: :default, payload: false)
logger.debug "debug message"
passthrough Format
The passthrough format is mostly used to just 'passthrough' all args to the device (proc, memory, file, etc.) without formatting anything.
This will simply provide an array of args.
Using this format will prevent newlines and does not recognize the :nl
/ :clr
option.
logger = ::SimpleLogger.new(:stdout, format: :passthrough, payload: false)
logger.debug "debug message"
plain Format
The plain format is only used to forward the provided data, without severity, time, etc.
This is the default behaviour of the SimpleLogger - which is used to build scene
, masks, ...
Using this format will prevent newlines and does not recognize the :nl
/ :clr
option.
logger = ::SimpleLogger.new(:stdout, format: :plain, payload: false)
logger.debug "debug message"
payload_logger = ::SimpleLogger.new(:stdout, format: :plain)
payload_logger.debug "debug message"
memory Format
The memory format is only used by the memory-device to store severity, time & data as an array.
Using this format will prevent newlines and does not recognize the :nl
/ :clr
option.
logger = ::SimpleLogger.new(:stdout, format: :memory, payload: false)
logger.debug "debug message"
datalog Format
The datalog format is used to store every log in a structured data.
For datalogs the colored output should also be disabled!
logger = ::SimpleLogger.new(:stdout, format: :datalog, payload: false, clr: false)
logger.debug "debug message"
Options
Independent of the builtins you can still provide custom options for the logger:
device
Provide a custom device.
logger = ::SimpleLogger.new(device: @my_custom_device)
logger = ::SimpleLogger.new(@my_custom_device)
clr
Disable colored output.
logger = ::SimpleLogger.new(clr: false)
logger = ::SimpleLogger.new(:stdout, clr: false)
payload
Disable payloads (from scenes).
logger = ::SimpleLogger.new(payload: false)
logger = ::SimpleLogger.new(:stdout, payload: false)
format
Provide a other format.
Possible values: `:default, :passthrough, :plain, :memory, :datalog```
logger = ::SimpleLogger.new(format: :default)
logger = ::SimpleLogger.new(:memory, format: :passthrough)
nl
Enable / disable NewLine for formatter.
logger = ::SimpleLogger.new(format: :default, nl: false, payload: false, clr: false)
logger.debug "debug 1"
logger.debug "debug 2"
proc (:proc-builtin ONLY)
Provide a callback for the :proc
builtin.
logger = ::SimpleLogger.new(:proc, proc: lambda{|data| ... })
stdout (:memory & Module-builtin ONLY)
Enable STDOUT as MultiDevice for the memory builtin.
logger = ::SimpleLogger.new(:memory, stdout: true)
logger = ::SimpleLogger.new(
::SimpleLogger::Devices::MultiDevice.new.
register(::SimpleLogger::Devices::MemoryDevice.new, ::SimpleLogger::Formatter.new(format: :memory)).
register(STDOUT, ::SimpleLogger::Formatter.new(format: :default, nl: true, clr: opts[:clr]))
)
mask
Provide custom mask options.
logger = ::SimpleLogger.new(mask: { char: '#', length: 50, clr: :purple })
logger.debug "debug text"
level
Change the severity level.
logger = ::SimpleLogger.new(level: :info)
logger.debug "debug text"
logger.info "info text"
formatter
Provide a custom formatter instance.
logger = ::SimpleLogger.new(formatter: My::Custom::Formatter.new)
inspect
Disable inspect for logger
logger = ::SimpleLogger.new(inspect: false)
logger.debug({a: {custom: 'object'}})
inspector
Provide a other inspector
method for the data-debug.
logger = ::SimpleLogger.new(inspector: :to_s)
logger.debug({ a: 1, b: 2 })
defaults
Device default options are still available: `shift_age, shift_size, progname, datetime_format, shift_period_suffix, binmode```
Scenes
The following PRE-defined scenes are available. (You can define your own scenes by using the class method `.scene```)
debug(data, subject = 'Debug')
info, warn, error, fatal, unknown, success (data, subject = 'name')
topic(subject)
theme(subject)
theme_result(result, status = nil)
theme_line
desc(description)
job(name)
sub_job(name)
result(result, status = nil)
job_result(name, result, status = nil)
sub_job_result(name, result, status = nil)
spec(status)
progress(name, perc)
processed(name, opts)
require 'simple_logger'
l = SimpleLogger.new(:stdout, payload: false)
l.processed("Process Alpha", timer: true) do
l.info "find files"
l.info "found 34 files"
l.processed("extracting ...", timer: true) do
l.info "10% done"
l.info "20% done"
l.info "100% done"
true
end
l.processed("transforming ...") do
l.error "bad memory"
l.info "rolling back"
false
end
nil
end
other useful methods
- line
- print
- nl
- model (rails only)
Docs
CHANGELOG
Contributing
Bug reports and pull requests are welcome on GitHub.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
A copy of the LICENSE can be found @ the docs.
Code of Conduct
Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the CODE OF CONDUCT.