Module: Chalk::Log

Includes:
ClassMethods
Defined in:
lib/chalk-log.rb,
lib/chalk-log/version.rb,
lib/chalk-log/errors.rb

Overview

Include ‘Chalk::Log` in a class or module to make that class (and all subclasses / includees / extendees) loggable. This creates a class and instance `log` method which you can call from within your loggable class.

Loggers are per-class and can be manipulated as you’d expect:

“‘ruby class A

include Chalk::Log

log.level = 'DEBUG'
log.debug('Now you see me!')
log.level = 'INFO'
log.debug('Now you do not!')

end “‘

You shouldn’t need to directly access any of the methods on ‘Chalk::Log` itself.

Defined Under Namespace

Modules: ClassMethods, Utils Classes: Error, InvalidArguments, Layout, Logger

Constant Summary collapse

VERSION =
'0.1.7'
LEVELS =

The set of available log methods. (Changing these is not currently a supported interface, though if the need arises it’d be easy to add.)

[:debug, :info, :warn, :error, :fatal].freeze

Class Method Summary collapse

Methods included from ClassMethods

#log

Class Method Details

.included(other) ⇒ Object

Method which goes through heroic efforts to ensure that the whole inclusion hierarchy has their ‘log` accessors.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chalk-log.rb', line 43

def self.included(other)
  if other == Object
    raise "You have attempted to `include Chalk::Log` onto Object. This is disallowed, since otherwise it might shadow any `log` method on classes that weren't expecting it (including, for example, `configatron.chalk.log`)."
  end

  # Already been through this ordeal; no need to repeat it. (There
  # shouldn't be any semantic harm to doing so, just a potential
  # performance hit.)
  return if @included.include?(other)
  @included << other

  # Make sure to define the .log class method
  other.extend(ClassMethods)

  # If it's a module, we need to make sure both inclusion/extension
  # result in virally carrying Chalk::Log inclusion downstream.
  if other.instance_of?(Module)
    other.class_eval do
      included = method(:included)
      extended = method(:extended)

      define_singleton_method(:included) do |other|
        other.send(:include, Chalk::Log)
        included.call(other)
      end

      define_singleton_method(:extended) do |other|
        other.send(:include, Chalk::Log)
        extended.call(other)
      end
    end
  end
end

.initObject

Public-facing initialization method for all ‘Chalk::Log` state. Unlike most other Chalk initializers, this will be automatically run (invoked on first logger instantiation). It is idempotent.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chalk-log.rb', line 81

def self.init
  return if @init
  @init = true

  # Load relevant configatron stuff
  Chalk::Config.register(File.expand_path('../../config.yaml', __FILE__),
    raw: true)

  # The assumption is you'll pipe your logs through something like
  # [Unilog](https://github.com/stripe/unilog) in production, which
  # does its own timestamping.
  Chalk::Config.register_raw(chalk: {log: {timestamp: STDERR.tty?}})

  ::Logging.init(*LEVELS)
  ::Logging.logger.root.add_appenders(
    ::Logging.appenders.stderr(layout: layout)
    )

  Chalk::Log::Logger.init
end

.layoutObject

The default layout to use for the root ‘Logging::Logger`.



103
104
105
# File 'lib/chalk-log.rb', line 103

def self.layout
  @layout ||= Chalk::Log::Layout.new
end

.message_prefixObject



112
113
114
# File 'lib/chalk-log.rb', line 112

def self.message_prefix
  LSpace[:'chalk.log.message_prefix']
end

.with_message_prefix(prefix, &blk) ⇒ Object

Adds a prefix to all logging within the current LSpace context.



108
109
110
# File 'lib/chalk-log.rb', line 108

def self.with_message_prefix(prefix, &blk)
  LSpace.with(:'chalk.log.message_prefix' => prefix, &blk)
end