Skip to content

Changing config on the fly

cihub edited this page Feb 12, 2012 · 15 revisions

The concept

Seelog was designed to be as stateless as possible: There is no initialization step, Seelog is immediately ready for logging right after importing its lib-path. If you want to refine your logger's configuration, you simply need to create a new xml config and call 'ReplaceLogger' func. The number of such reconfigurations is unlimited.

We have a test which demonstrates the logger switching in 1000 logging goroutines after each message.

It is located here: stresstest

Result of one of latest stress test runs:

~/.../github.com/cihub/seelog-examples/stresstest$ ./stresstest
Logger replaced 100001 times. Average replacement frequency: 4757.623047 times / second. 
Output log is consistent: no log messages are missing or come in incorrect order.
PASS! Output is valid

How to change config on the fly

The short answer is simple: call ReplaceLogger with a new logger at any moment of time. But the main thing here is to provide such functionality to your application users. For example, if you would like to change logging configuration of your server on-the-fly without restarting it, you need to create some kind of web-interface with a config text box and a 'submit' button that would consequently call 'log.LoggerFromBytes(...)' and 'log.ReplaceLogger'. If the LoggerFromBytes returned an error, like config syntax error, you should create a corresponding error web-page response.

Another option is to check whether the config file changed, and if so, create a config from new file bytes and call 'ReplaceLoger' if new config doesn't contain any errors. We are going to provide such config update checker as a helper utility later.