Skip to content

Use as ServerEngine logger

Toshimitsu Takahashi edited this page Sep 3, 2017 · 3 revisions

ServerEngine is the daemon engine of fluentd.

  • Defining a custom logger adapts Ougai::Logger as ServerEngine::DaemonLogger
  • A child logger is useful to output with worker id.
require 'rubygems'
require 'serverengine'
require 'ougai'

# Dummy worker
module MyWorker
  def run
    log = logger.child(worker: worker_id)
    log.info 'start'

    @count = 1
    until @stop
      log.trace 'step', count: @count
      sleep 1
      @count += 1
    end

    log.info 'stop'
  end

  def stop
    @stop = true
  end
end

# Remove log file header like 'Logfile created on...'
class Logger::LogDevice
  def add_log_header(file); end
end

class MyLogger < Ougai::Logger
  def initialize(logdev, config = {})
    super(logdev,
          config[:log_rotate_age],
          config[:log_rotate_size],
          level: config[:log_level])
  end

  # ServerEngine trys to set logdev via the attribute
  def logdev=(logdev); end
end

se = ServerEngine.create(nil, MyWorker, {
  daemonize: true,
  logger_class: MyLogger,
  log: 'myserver.log',
  log_level: 'trace',
  log_rotate_age: 3,
  log_rotate_size: 4096,
  pid_path: 'myserver.pid',
  worker_type: 'process',
  workers: 4
})
se.run
Clone this wiki locally