Skip to content

Commit eef2697

Browse files
Update documentation about plog initialization (#287)
1 parent 9deafbf commit eef2697

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

README.md

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Pretty powerful logging library in about 1000 lines of code [![CI](https://githu
1616
- [Usage](#usage)
1717
- [Step 1: Adding includes](#step-1-adding-includes)
1818
- [Step 2: Initialization](#step-2-initialization)
19+
- [RollingFileInitializer](#rollingfileinitializer)
20+
- [ConsoleInitializer](#consoleinitializer)
21+
- [Manual initialization (Init.h)](#manual-initialization-inith)
1922
- [Step 3: Logging](#step-3-logging)
2023
- [Basic logging macros](#basic-logging-macros)
2124
- [Conditional logging macros](#conditional-logging-macros)
@@ -32,8 +35,24 @@ Pretty powerful logging library in about 1000 lines of code [![CI](https://githu
3235
- [Logger](#logger)
3336
- [Record](#record)
3437
- [Formatter](#formatter)
38+
- [TxtFormatter](#txtformatter)
39+
- [TxtFormatterUtcTime](#txtformatterutctime)
40+
- [CsvFormatter](#csvformatter)
41+
- [CsvFormatterUtcTime](#csvformatterutctime)
42+
- [FuncMessageFormatter](#funcmessageformatter)
43+
- [MessageOnlyFormatter](#messageonlyformatter)
3544
- [Converter](#converter)
45+
- [UTF8Converter](#utf8converter)
46+
- [NativeEOLConverter](#nativeeolconverter)
3647
- [Appender](#appender)
48+
- [RollingFileAppender](#rollingfileappender)
49+
- [ConsoleAppender](#consoleappender)
50+
- [ColorConsoleAppender](#colorconsoleappender)
51+
- [AndroidAppender](#androidappender)
52+
- [EventLogAppender](#eventlogappender)
53+
- [DebugOutputAppender](#debugoutputappender)
54+
- [ArduinoAppender](#arduinoappender)
55+
- [DynamicAppender](#dynamicappender)
3756
- [Miscellaneous notes](#miscellaneous-notes)
3857
- [Lazy stream evaluation](#lazy-stream-evaluation)
3958
- [Stream improvements over std::ostream](#stream-improvements-over-stdostream)
@@ -210,13 +229,15 @@ At first your project needs to know about plog. For that you have to:
210229
2. Add `#include <plog/Log.h>` into your cpp/h files (if you have precompiled headers it is a good place to add this include there)
211230
212231
## Step 2: Initialization
213-
The next step is to initialize the [Logger](#logger). This is done by the following `plog::init` function:
232+
To use plog, you must initialize the logger by including the appropriate header and calling the corresponding `plog::init` overload:
214233
215234
```cpp
216-
Logger& init(Severity maxSeverity, const char/wchar_t* fileName, size_t maxFileSize = 0, int maxFiles = 0);
235+
Logger& init(Severity maxSeverity, ...
217236
```
218237

219-
`maxSeverity` is the logger severity upper limit. All log messages have its own severity and if it is higher than the limit those messages are dropped. Plog defines the following severity levels:
238+
`maxSeverity` is the logger severity upper limit. Log messages with a severity value higher (less severe) than the limit are dropped.
239+
240+
Plog defines the following severity levels:
220241

221242
```cpp
222243
enum Severity
@@ -233,26 +254,70 @@ enum Severity
233254

234255
> **Note** Messages with severity level `none` will always be printed.
235256
236-
The log format is determined automatically by `fileName` file extension:
237-
238-
- .csv => [CSV format](#csvformatter)
239-
- anything else => [TXT format](#txtformatter)
257+
Plog provides several convenient initializer functions to simplify logger setup for common use cases. These initializers configure the logger with typical appenders and formatters, so you can get started quickly without manually specifying all template parameters.
240258

241-
The rolling behavior is controlled by `maxFileSize` and `maxFiles` parameters:
259+
### RollingFileInitializer
260+
Use this when you want to log to a file with automatic rolling (rotation) based on size and count. Add `#include <plog/Initializers/RollingFileInitializer.h>` and call `init`:
242261

243-
- `maxFileSize` - the maximum log file size in bytes
244-
- `maxFiles` - a number of log files to keep
262+
```cpp
263+
Logger& init(Severity maxSeverity, const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0);
264+
```
245265
246-
If one of them is zero then log rolling is disabled.
266+
- The log format is determined by the file extension:
267+
- `.csv` → [CSV format](#csvformatter)
268+
- anything else → [TXT format](#txtformatter)
269+
- You can override the format by specifying a formatter as a template parameter, e.g. `plog::init<plog::CsvFormatter>(...)`.
270+
- Rolling is controlled by `maxFileSize` (bytes) and `maxFiles` (number of files to keep). If either is zero, rolling is disabled.
247271
248-
Sample:
272+
Example:
249273
250274
```cpp
275+
#include <plog/Log.h>
276+
#include <plog/Initializers/RollingFileInitializer.h>
277+
251278
plog::init(plog::warning, "c:\\logs\\log.csv", 1000000, 5);
252279
```
253280

254281
Here the logger is initialized to write all messages with up to warning severity to a file in csv format. Maximum log file size is set to 1'000'000 bytes and 5 log files are kept.
255282

283+
### ConsoleInitializer
284+
Use this to log to the console (stdout or stderr) with color output. Add `#include <plog/Initializers/ConsoleInitializer.h>` and call `init`:
285+
286+
```cpp
287+
Logger& init(Severity maxSeverity, OutputStream outputStream)
288+
```
289+
290+
- By default it uses [TXT format](#txtformatter) but it can be overriden by specifying a formatter as a template parameter, e.g. `plog::init<plog::CsvFormatter>(...)`.
291+
- `outputStream` chooses the output stream: `plog::streamStdOut` or `plog::streamStdErr`.
292+
293+
Example:
294+
295+
```cpp
296+
#include <plog/Log.h>
297+
#include <plog/Initializers/ConsoleInitializer.h>
298+
299+
plog::init<plog::TxtFormatter>(plog::error, plog::streamStdErr); // logs error and above to stderr
300+
```
301+
302+
### Manual initialization (Init.h)
303+
For advanced or custom setups add `#include <plog/Init.h>` and call `init`:
304+
305+
```cpp
306+
Logger& init(Severity maxSeverity = none, IAppender* appender = NULL);
307+
```
308+
309+
You must construct and manage the appender yourself.
310+
311+
Example:
312+
313+
```cpp
314+
#include <plog/Log.h>
315+
#include <plog/Init.h>
316+
317+
static plog::ConsoleAppender<plog::TxtFormatter> appender;
318+
plog::init(plog::info, &appender); // logs info and above to the specified appender
319+
```
320+
256321
> **Note** See [Custom initialization](#custom-initialization) for advanced usage.
257322
258323
## Step 3: Logging

0 commit comments

Comments
 (0)