The easy way, use Named Formatters and Destinations
You use a string to specify Formatters, and a string to specify Destinations. Thus, you use the writer::named_write.
First, the examples: example 1, example 2
The manual way
First, the examples: example 1, example 2
Scenario 1 should be the most common.
This usage:
- You have multiple levels (in this example: debug < info < error)
- You want to format the message before it's written (in this example: prefix it by time, by index, and append newline to it)
- You have one log, which writes to several log destinations (in this example: the console, the debug output window, and a file)
In this example, all output will be written to the console, debug output window, and "out.txt" file. It will look similar to this one:
21:03.17.243 [1] this is so cool 1
21:03.17.243 [2] first error 2
21:03.17.243 [3] hello, world
21:03.17.243 [4] second error 3
21:03.17.243 [5] good to be back ;) 4
21:03.17.243 [6] third error 5
Click to see the code
This usage:
- You have one logger
- You have one filter, which can be turned on or off
- You want to format the message before it's written
- The logger has several log destinations
- The output goes to console, debug output window, and a file called out.txt
- Formatting - prefix each message by its index, and append newline
Optimizations:
- use a cache string (from optimize namespace), in order to make formatting the message faster
In this example, all output will be written to the console, debug window, and "out.txt" file. It will be:
[1] this is so cool 1
[2] this is so cool again 2
[3] hello, world
[4] good to be back ;) 3
Click to see the code
This usage:
- Fastest. It does not use formatters and destinations (thus, the flexibility that comes with formatters/destinations) is gone
- You have one filter, which can be turned on or off
- You have 2 loggers: app and err.
- The app writes to console, the err writes to "err.txt" file
- the output is "dump" - no usage of << operator
Here's what the output will be:
The console:
this is so cool
hello, world
good to be back ;)
The err.txt file:
Click to see the code
This usage:
- You have several loggers
- You have one filter, which can be turned on or off
- You want to format the message before it's written
- Each logger has several log destinations
- The logger and filter are specified using the boost::logging::scenario namespace
- the filter is always accurate (but slow)
- the filter does not use levels
- the logger favors speed (on a dedicated thread)
- the logger is initialized once, when only one thread is running
Optimizations:
- use a cache string (from optimize namespace), in order to make formatting the message faster
Logs:
- Error messages go into err.txt file
- formatting - prefix each message by time, index, and append newline
- Info output goes to console, and a file called out.txt
- formatting - prefix each message by time and append newline
- Debug messages go to the debug output window, and the console
- formatting - prefix each message by time, and append newline
Here's how the output will look like:
The debug output window:
18:59.24 this is so cool 1
18:59.24 this is so cool again 2
The console:
18:59.24 this is so cool 1
18:59.24 this is so cool again 2
18:59.24 hello, world
18:59.24 good to be back ;) 4
The out.txt file:
18:59.24 hello, world
18:59.24 good to be back ;) 4
The err.txt file
18:59.24 [1] first error 3
18:59.24 [2] second error 5
Click to see the code
Copyright John Torjo © 2007
Have a question/ suggestion/ comment? Send me feedback