Skip to content

Instantly share code, notes, and snippets.

@zhouxiaozhen-github
Last active April 18, 2019 07:05
Show Gist options
  • Save zhouxiaozhen-github/ba26f4ff626a138cb4dd5c62d5837c6d to your computer and use it in GitHub Desktop.
Save zhouxiaozhen-github/ba26f4ff626a138cb4dd5c62d5837c6d to your computer and use it in GitHub Desktop.
Rails 做一个自己的logger

Rails 开发很多时候想要吧一些独立的业务处理单独写在一个 log 文件.

Rails 想要新建一个单独的 log 文件是很简单的。

这里我利用单独 logger 来纪录 resque worker 的一些事情。

首先建立一个 logger model

class WorkerLogger < Logger

def format_message(severity, timestamp, progname, msg)

"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"

end

end

然后创建一个 initializer

worker_logfile = File.open("#{Rails.root}/log/worker.log", 'a')

worker_logfile.sync = true

WORKER_LOG = WorkerLogger.new(worker_logfile)

然后就可以到处用了

WORKER_LOG.info "Something happened!"

WORKER_LOG.debug "Something bad really happened!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment