module Chook::Server::Log

This module defines our custom Logger instance from the config settings and makes it available in the .logger module method, which is used anywhere outside of a route (inside of a route, the logger method is locally available)

General access to the logger:

from anywhere in Chook, as long as a server is running, the Logger
instance is available at Chook.logger or Chook::Server::Log.logger

Logging from inside a route:

inside a Sinatra route, the local `logger` method returnes the
Logger instance.

Logging from an internal handler:

In an internal WebHook handler, starting with `Chook.event_handler do |event|`
the logger should be accesses via the event's own wrapper: event.logger
Each message will be prepended with the event'd ID

Logging from an external handler:

from an external handler you can POST a JSON formatted log entry to
http(s)://chookserver/log. The body must be a JSON object with 2 keys:
'level' and 'message', with non-empty strings.
The level must be one of: fatal, error, warn, info, or debug. Any other
value as the level will always be logged regardless of current logging
level. NOTE: if your server requires authentication, you must provide it
when using this route.

Here's an example with curl, split to multi-line for clarity:

curl -H “Content-Type: application/json” \

-X POST \
--data '{"level":"debug", "message":"It Worked"}' \
https://user:passwd@chookserver.myorg.org:443/log

Constants

DEFAULT_FILE
DEFAULT_LEVEL
DEFAULT_MAX_MEGS
DEFAULT_TO_KEEP
LOGSTREAM_CLOSED_PFX

the clients will recognize M3_LOG_STREAM_CLOSED and stop trying to connect via ssh.

LOGSTREAM_DATA_PFX

log Streaming ServerSent Events data lines always start with this

LOGSTREAM_KEEPALIVE_MAX
LOGSTREAM_KEEPALIVE_MSG

Send this to the clients at least every LOGSTREAM_KEEPALIVE_MAX secs even if there's no data for the stream

LOG_LEVELS

mapping of integer levels to symbols

Public Class Methods

clean_log_streams() click to toggle source
# File lib/chook/server/log.rb, line 188
def self.clean_log_streams
  log_streams.delete_if do |stream, ip|
    if stream.closed?
      logger.debug "Removing closed log stream for #{ip}"
      true
    else
      false
    end # if
  end # delete if
end
log_streams() click to toggle source

a Hash of registered log streams streams are keys, valus are their IP addrs see the `get '/subscribe_to_log_stream'` route

# File lib/chook/server/log.rb, line 184
def self.log_streams
  @log_streams ||= {}
end
logger() click to toggle source

general access to the logger as Chook::Server::Log.logger

# File lib/chook/server/log.rb, line 176
def self.logger
  @logger ||= startup
end
startup(level = Chook.config.log_level) click to toggle source

Create the logger, make the first log entry for this run, and return it so it can be used by the server when it does `set :logger, Log.startup(@log_level)`

# File lib/chook/server/log.rb, line 134
def self.startup(level = Chook.config.log_level)
  # create the logger using a LogFileWithStream instance
  @logger =
    if Chook.config.logs_to_keep && Chook.config.logs_to_keep > 0
      Logger.new(
        LogFileWithStream.new(Chook.config.log_file, 'a'),
        Chook.config.logs_to_keep,
        (Chook.config.log_max_megs * 1024 * 1024)
      )
    else
      Logger.new(LogFileWithStream.new(Chook.config.log_file, 'a'))
    end

  # date and line format
  @logger.datetime_format = '%Y-%m-%d %H:%M:%S'

  @logger.formatter = proc do |severity, datetime, _progname, msg|
    "#{datetime}: [#{severity}] #{msg}\n"
  end

  # level
  level &&= Chook::Procs::STRING_TO_LOG_LEVEL.call level
  level ||= Chook.config.log_level
  level ||= DEFAULT_LEVEL
  @logger.level = level

  # first startup entry
  @logger.unknown "Chook Server v#{Chook::VERSION} starting up. PID: #{$PROCESS_ID}, Port: #{Chook.config.port}, SSL: #{Chook.config.use_ssl}"

  # if debug, log our config
  if level == Logger::DEBUG
    @logger.debug 'Config: '
    Chook::Configuration::CONF_KEYS.keys.each do |key|
      @logger.debug "  Chook.config.#{key} = #{Chook.config.send key}"
    end
  end

  # return the logger, the server uses it as a helper
  @logger
end