Module: Chook::Server::Log

Defined in:
lib/chook/server/log.rb

Overview

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:[email protected]:443/log

Defined Under Namespace

Classes: LogFileWithStream

Constant Summary collapse

LOG_LEVELS =

mapping of integer levels to symbols

{
  fatal: Logger::FATAL,
  error: Logger::ERROR,
  warn: Logger::WARN,
  info: Logger::INFO,
  debug: Logger::DEBUG
}.freeze
LOGSTREAM_DATA_PFX =

log Streaming ServerSent Events data lines always start with this

'data:'.freeze
LOGSTREAM_KEEPALIVE_MSG =

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

"#{LOGSTREAM_DATA_PFX} I'm Here!\n\n".freeze
LOGSTREAM_KEEPALIVE_MAX =
10
LOGSTREAM_CLOSED_PFX =

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

"#{LOGSTREAM_DATA_PFX} M3_LOG_STREAM_CLOSED:".freeze
DEFAULT_FILE =
Pathname.new '/var/log/chook-server.log'
DEFAULT_MAX_MEGS =
10
DEFAULT_TO_KEEP =
10
DEFAULT_LEVEL =
Logger::INFO

Class Method Summary collapse

Class Method Details

.clean_log_streamsObject



188
189
190
191
192
193
194
195
196
197
# 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_streamsObject

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



184
185
186
# File 'lib/chook/server/log.rb', line 184

def self.log_streams
  @log_streams ||= {}
end

.loggerObject

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



176
177
178
# File 'lib/chook/server/log.rb', line 176

def self.logger
  @logger ||= startup
end

.startup(level = Chook.config.log_level) ⇒ Object

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)`



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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