Skip to content

Instantly share code, notes, and snippets.

@Lupeipei
Last active March 8, 2018 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lupeipei/7297b1a784e7bb4da39eeb7d26c8d834 to your computer and use it in GitHub Desktop.
Save Lupeipei/7297b1a784e7bb4da39eeb7d26c8d834 to your computer and use it in GitHub Desktop.
配置log4r
### 修改config/application.rb
require 'rails/all'
# add these line for log4r
require 'log4r'
require 'log4r/yamlconfigurator'
require 'log4r/outputter/datefileoutputter'
Bundler.require(:default, Rails.env) if defined?(Bundler)
module Zurich
class Application < Rails::Application
#...
# assign log4r's logger as rails' logger.
include Log4r
log4r_config= YAML.load_file(File.join(File.dirname(__FILE__),"log4r.yml"))
YamlConfigurator.decode_yaml( log4r_config['log4r_config'] )
config.logger = Log4r::Logger[Rails.env]
end
end
### 新建一个log4r的配置文件: config/log4r.yml
log4r_config:
# define all loggers ...
loggers:
- name : production
level : WARN
trace : 'false'
outputters :
- datefile
- name : development
level : DEBUG
trace : 'true'
outputters :
- datefile
# define all outputters (incl. formatters)
outputters:
- type: DateFileOutputter
name: datefile
dirname: "log"
filename: "my_app.log" # notice the file extension is needed!
formatter:
date_pattern: '%H:%M:%S'
pattern : '%d %l: %m '
type : PatternFormatter
### 新建文件:config/initializers/log4r_patch_for_rails4.rb
class Log4r::Logger
def formatter()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment