tinylog-org / tinylog

tinylog is a lightweight logging framework for Java, Kotlin, Scala, and Android
https://tinylog.org
Apache License 2.0
699 stars 82 forks source link

Log to separate log files using tinyLog #353

Closed msingh-ms closed 1 year ago

msingh-ms commented 1 year ago

I'm working on a project with multiple modules for end user and logs for different modules must be logged to separate log files. Is it possible to achieve this using tinyLog?

For example, logs for creating a new user in the project must go to newuser.log

and logs for deleting an existing user in the project must go to deleteuser.log

pmwmedia commented 1 year ago

You can use tagged loggers to archive this:

TaggedLogger newUserLogger = Logger.tag("NEWUSER");
TaggedLogger deleteUserLogger = Logger.tag("DELETEUSER");

Then you can bind your file writers to these tags:

writer1      = file
writer1.tag  = NEWUSER
writer1.file = newuser.log

writer2      = file
writer2.tag  = DELETEUSER
writer2.file = deleteuser.log

Documentation about tags:

https://tinylog.org/v2/logging/#tags https://tinylog.org/v2/configuration/#tags

msingh-ms commented 1 year ago

Thank you so much!

msingh-ms commented 1 year ago

Asking another question here as its related, is it possible to add another writer after first Log has been written and config has been frozen?

Something like:

    Configuration.set("writer", "rolling file");
    Configuration.set("writer.file", "log.txt");
    Configuration.set("writer.tag", "log");
    Configuration.set("writer.append", "true");
    Configuration.set("writer.policies", "daily");
    Logger.tag("log").info("Hello world!");
    Map configUpdate = new HashMap();
    configUpdate.put("writer1", "rolling file");
    configUpdate.put("writer1.file", "log1.txt");
    configUpdate.put("writer1.tag", "log1");
    configUpdate.put("writer1.append", "true");
    configUpdate.put("writer1.policies", "daily");
    ReconfigurableLoggingProvider.reload(configUpdate);
    Logger.tag("log1").info("Hello world 1!");
    Logger.tag("log").info("Hello world again!");

Basically, I'm looking for a thread safe approach as logging to existing writers may be in progress while configuration is being updated using CustomLoggingProvider