| 1 | SimpleLogger |
| 2 | ============ |
| 3 | |
| 4 | Looking for a simple logger for your C++ project? `SimpleLogger` might be for you. |
| 5 | |
| 6 | # Features |
| 7 | |
| 8 | * Based on RAII |
| 9 | * Configurable level symbols |
| 10 | * Datetime, ISO Datetime, EPOCH, and custom timestamp formats |
| 11 | * Logging levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, `Fatal` |
| 12 | * Log to file and/or console |
| 13 | * Thread-safe |
| 14 | * Uses streams (<< operator) |
| 15 | * Very easy to use |
| 16 | |
| 17 | # Installation |
| 18 | |
| 19 | ## Use from sources |
| 20 | |
| 21 | Just add `src/simple_logger.hpp` and `src/simple_logger.cpp` to your project and start using it! |
| 22 | |
| 23 | ## Use as a CMake subproject (Recommended) |
| 24 | |
| 25 | Copy contents of `SimpleLogger` under your main project (or clone as a Git submodule). |
| 26 | |
| 27 | In your `CMakeLists.txt`: |
| 28 | |
| 29 | ``` |
| 30 | add_subdirectory(SimpleLogger EXCLUDE_FROM_ALL) |
| 31 | include_directories(SimpleLogger/src) |
| 32 | ``` |
| 33 | |
| 34 | Link to the library: |
| 35 | |
| 36 | ``` |
| 37 | target_link_libraries(${YOUR_TARGET_NAME} SimpleLogger_static) |
| 38 | ``` |
| 39 | |
| 40 | In your code: |
| 41 | |
| 42 | ``` |
| 43 | #include "simple_logger.hpp" |
| 44 | ``` |
| 45 | |
| 46 | ## Use as a library |
| 47 | |
| 48 | Build and install: |
| 49 | |
| 50 | `$ mkdir build && cd build` |
| 51 | |
| 52 | `$ cmake ..` |
| 53 | |
| 54 | `$ make` |
| 55 | |
| 56 | `$ sudo make install` |
| 57 | |
| 58 | Link to `libSimpleLogger_static.a` or `libSimpleLogger.so`. |
| 59 | |
| 60 | # Examples |
| 61 | |
| 62 | Note: instead of `Logger` class you can simply use `L`. |
| 63 | |
| 64 | ## Log to console |
| 65 | |
| 66 | ``` |
| 67 | using juzzlin::L; |
| 68 | |
| 69 | L().info() << "Something happened"; |
| 70 | ``` |
| 71 | |
| 72 | Outputs something like this: |
| 73 | |
| 74 | `Sat Oct 13 22:38:42 2018 I: Something happened` |
| 75 | |
| 76 | ## Log to file and console |
| 77 | |
| 78 | ``` |
| 79 | using juzzlin::L; |
| 80 | |
| 81 | L::initialize("/tmp/myLog.txt"); |
| 82 | |
| 83 | L().info() << "Something happened"; |
| 84 | ``` |
| 85 | |
| 86 | ## Log only to file |
| 87 | |
| 88 | ``` |
| 89 | using juzzlin::L; |
| 90 | |
| 91 | L::initialize("/tmp/myLog.txt"); |
| 92 | L::enableEchoMode(false); |
| 93 | |
| 94 | L().info() << "Something happened"; |
| 95 | ``` |
| 96 | |
| 97 | ## Set logging level |
| 98 | |
| 99 | ``` |
| 100 | using juzzlin::L; |
| 101 | |
| 102 | L::setLoggingLevel(L::Level::Debug); |
| 103 | |
| 104 | L().info() << "Something happened"; |
| 105 | L().debug() << "A debug thing happened"; |
| 106 | ``` |
| 107 | |
| 108 | Outputs something like this: |
| 109 | |
| 110 | `Sat Oct 13 22:38:42 2018 I: Something happened` |
| 111 | |
| 112 | `Sat Oct 13 22:38:42 2018 D: A debug thing happened` |
| 113 | |
| 114 | ## Log with a tag |
| 115 | |
| 116 | ``` |
| 117 | using juzzlin::L; |
| 118 | |
| 119 | L::setLoggingLevel(L::Level::Info); |
| 120 | |
| 121 | L("MyTag").info() << "Something happened"; |
| 122 | ``` |
| 123 | |
| 124 | Outputs something like this: |
| 125 | |
| 126 | `Sat Oct 13 22:38:42 2018 I: MyTag: Something happened` |
| 127 | |
| 128 | ## Set custom level symbols |
| 129 | |
| 130 | ``` |
| 131 | using juzzlin::L; |
| 132 | |
| 133 | L::setLoggingLevel(L::Level::Debug); |
| 134 | L::setLevelSymbol(L::Level::Debug, "<DEBUG>"); |
| 135 | |
| 136 | L().debug() << "A debug thing happened"; |
| 137 | ``` |
| 138 | |
| 139 | Outputs something like this: |
| 140 | |
| 141 | `Sat Oct 13 22:38:42 2018 <DEBUG> A debug thing happened` |
| 142 | |
| 143 | ## Set timestamp mode and optional timestamp separator |
| 144 | |
| 145 | Possible timestamp modes: `None`, `EpochSeconds`, `EpochMilliseconds`, `EpochMicroseconds`, `DateTime`, `ISODateTime`. |
| 146 | |
| 147 | ``` |
| 148 | using juzzlin::L; |
| 149 | |
| 150 | L::setTimestampMode(L::TimestampMode::EpochMilliseconds); |
| 151 | L::setTimestampSeparator(" ## "); |
| 152 | |
| 153 | L().info() << "Something happened"; |
| 154 | ``` |
| 155 | |
| 156 | Outputs something like this: |
| 157 | |
| 158 | `1562955750677 ## I: Something happened` |
| 159 | |
| 160 | ## Set custom timestamp format |
| 161 | |
| 162 | By setting a custom timestamp format the timestamp mode is set to `Custom`: |
| 163 | |
| 164 | ``` |
| 165 | using juzzlin::L; |
| 166 | |
| 167 | L::setCustomTimestampFormat("%H:%M:%S_%Y-%m-%d"); |
| 168 | L::setTimestampSeparator(" ## "); |
| 169 | |
| 170 | L().info() << "Something happened"; |
| 171 | ``` |
| 172 | |
| 173 | Outputs something like this: |
| 174 | |
| 175 | `12:34:58_2024-07-06 ## I: Something happened` |
| 176 | |
| 177 | ## Set custom output stream |
| 178 | |
| 179 | ``` |
| 180 | using juzzlin::L; |
| 181 | |
| 182 | std::stringstream ssI; |
| 183 | L::setStream(L::Level::Info, ssI); |
| 184 | ``` |
| 185 | |
| 186 | # Requirements |
| 187 | |
| 188 | C++17 |
| 189 | |
| 190 | # Licence |
| 191 | |
| 192 | MIT |
| 193 | |
| 194 | # Projects currently using SimpleLogger |
| 195 | |
| 196 | * https://github.com/juzzlin/DustRacing2D |
| 197 | |
| 198 | * https://github.com/juzzlin/Heimer |
| 199 |