emilk / loguru

A lightweight C++ logging library
The Unlicense
1.76k stars 256 forks source link

Multi file logging #235

Closed marincovic closed 1 year ago

marincovic commented 1 year ago

Hello team,

Is it possible to use Loguru to log to 2 files? I have seen that we can use it to open 2 files and log depending on the verbosity level. But is it possible to open 2 files and then tell the log function to what file do I wish to write?

BR// Marin

emilk commented 1 year ago

I don't quite understand the question, but the answer is yes.

You can call add_file twice to log to two different files.

Or you can use add_callback to do whatever you want.

See https://emilk.github.io/loguru/index.html#callbacks

marincovic commented 1 year ago

Let me give an example: Lets say we have 2 classes of one application. Frontend and Backend and each should have its own log file. We can add 2 files using the following code:

loguru::add_file("Frontend.log", loguru::Append, loguru::Verbosity_MAX)
loguru::add_file("Backend.log", loguru::Append, loguru::Verbosity_MAX)

Now when I want to log in Frontend class I would like to be able to say : LOG_F(INFO,"AN ERROR JUST HAPPENED","Frontend.log"); And if anything happens in the Backend I would like to tell the log to log directly into the appropriate file.

I hope this has helped in clarifying the request.

BR// Marin