I am creating my logger in the following way:
Logging.color_scheme( 'bright',
:levels => {
:info => :green,
:warn => :yellow,
:error => :red,
:fatal => [:white, :on_red]
},
:date => :blue,
:logger => :cyan,
:message => :magenta
)
@logger = Logging.logger['main_logger']
@logger.add_appenders(
Logging.appenders.stdout,
Logging.appenders.file('file',
:filename => LOG_FILE_PATH,
:layout => Logging.layouts.pattern(:pattern => '[%d] %-5l %c: %m\n')
)
)
I would like to add the thread id to each log message (Thread.current.object_id), is there a way i can do it ?
You have to use the Mapped Diagnostic Context 'mdc' (see https://github.com/TwP/logging/blob/master/examples/mdc.rb)
Here is what I do :
Logging.appenders.stdout(
'stdout',
:layout => Logging.layouts.pattern(
:pattern => '%d %-5l [%X{tid}]: %m\n',
:date_pattern => "%d/%m/%Y %H:%M:%S",
:color_scheme => 'bright'
),
)
and in your thread, indicate the value of tide field like that :
Logging.mdc['tid'] = Thread.current.object_id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With