How to customize Rails log messages to JSON format -


i need customize log messages json format in rails app.

to illustrate, log messages app produces this:

i, [2015-04-24t11:52:06.612993 #90159]  info -- : started "/time_entries" ::1 @ 2015-04-24 11:52:06 -0400 

as result, log messages line above written log.

i need change format json. how can make log output formatted following example?

{   "type" : "info",   "time" : "2015-04-24t11:52:06.612993",   "message" : "started "/time_entries" ::1 @ 2015-04-24 11:52:06 -0400" } 

note: doesn't need pretty printed example, has have json format.

you can configure rails specify own log formatter:

config.log_formatter defines formatter of rails logger. option defaults instance of activesupport::logger::simpleformatter modes except production, defaults logger::formatter.

you can provide own class output log information:

class mysimpleformatter < activesupport::logger::simpleformatter   def call(severity, timestamp, _progname, message)     {        type: severity,       time: timestamp,       message: message     }.to_json   end end 

to configure new class you'd need add config line:

config.log_formatter = mysimpleformatter.new 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -