Module: NRSER::Log::Formatters::Mixin

Included in:
Color
Defined in:
lib/nrser/log/formatters/mixin.rb

Overview

A mix-in to include in SemanticLogger::Formatter::Default and subclasses to improve configurability.

Defined Under Namespace

Classes: BodyTokens, HeaderTokens, Tokens

Instance Method Summary collapse

Instance Method Details

#body(*tokens) ⇒ BodyTokens

Get or set the body section tokens.

Just like #header, which has details and examples, but for the “body” section of log messages (message, payload, exception).

Parameters:

  • tokens (Array<Symbol>)

    When not empty, sets the body to those tokens in that order.

    When empty, the current body is returned.

Returns:

See Also:



213
214
215
216
217
218
219
# File 'lib/nrser/log/formatters/mixin.rb', line 213

def body *tokens
  if tokens.empty?
    @body ||= BodyTokens.new
  else
    @body = BodyTokens.new tokens
  end
end

#body=(tokens) ⇒ HeaderTokens

Set the body section format tokens.

See #header for details and examples of how the header works.

Parameters:

  • tokens (Array<Symbol>)

    Token symbols to set as the body format.

Returns:

See Also:



234
235
236
# File 'lib/nrser/log/formatters/mixin.rb', line 234

def body= tokens
  @body = BodyTokens.new tokens
end

#header(*tokens) ⇒ HeaderTokens

Get or set the header “tokens” - SemanticLogger::Formatters::Default method names in the order their responses should be joined to form the header section of formatted log messages (time, level, name, etc.).

Examples:

Getting the HeaderTokens

# You won't see much unless logging is setup
NRSER::Log.setup! dest: $stdout, level: :info

# Full object path to HeaderTokens - there's also a {NRSER::Log.header}
# shortcut
NRSER::Log.appender.formatter.header
# => [:time, :level, :process_info, :tags, :named_tags, :duration, :name]

NRSER.logger.info "Hey yo!"
# > 2018-04-12 23:43:24.982040 INFO [19301:main] NRSER
# > -- Hey yo!
# >
# => true

Setting the HeaderTokens

# You won't see much unless logging is setup
NRSER::Log.setup! dest: $stdout, level: :info

# Set a very simple header
NRSER::Log.header :level, :name
# => [:level, :name]

# And notice the reduced log header
NRSER.logger.info "Hey yo!"
# > INFO NRSER
# > -- Hey yo!
# >
# => true

Parameters:

  • tokens (Array<Symbol>)

    Optional list of token symbols to set as the header format.

    When empty, the method works as a getter, returning the current header format tokens.

Returns:



172
173
174
175
176
177
178
# File 'lib/nrser/log/formatters/mixin.rb', line 172

def header *tokens
  if tokens.empty?
    @header ||= HeaderTokens.new
  else
    @header = HeaderTokens.new tokens
  end
end

#header=(tokens) ⇒ HeaderTokens

Set the header section format tokens.

See #header for details and examples of how the header works.

Parameters:

  • tokens (Array<Symbol>)

    Token symbols to set as the header format.

Returns:

See Also:



193
194
195
# File 'lib/nrser/log/formatters/mixin.rb', line 193

def header= tokens
  @header = HeaderTokens.new tokens
end