Simple threading safe and lightweight logger for c++ projects that uses namespace logger
Initialize Log instance and call for the log methods.
Example:
logger::Log log;
log.error("Something is going wrong...");-
std::string path - the path to the file the data will be append to
-
std::string name - name you specify for your purposes (to recognize different modules that could write to the same file location for example)
-
int allowance - (optional) number that specify which types of logs records will be wrote to the log file.
0bit of this number responsible for theerrorlogs,1'st bit responsible for thewarningrecords and2'nd but responsible for theinforecords.Example:
logger::Log log(5); // (101) Will pass only Error and Info records
There is also local variables that corresponds to each log record type available. The next expression works the same as previous one
Example:
logger::Log log(logger::L_ERROR | logger::L_INFO); // L_WARNING for warnings
- Log([int allowance]) - default cinstructor
The
main.logfile will be created at the root folder.nameis not specified. The name is not specified. Default value forallowanceis7 - Log(std::string path[, int allowance]) -
nameis not specified, default value forallowanceis7 - Log(std::string path, std::string name[, int allowance]) - Default value for
allowanceis7
-
void error(std::string message)- Writes the error record to the log file -
void warning(std::string message)- Writes the warning record to the log file -
void info(std::string message)- Writes the info record to the log file -
void log(int type, std::string message)- Write the corresponded record type to the log file. -
void flush()- flush the buffer for the owning log fileExample:
logger::Log log; log.error("Error message"); log.info("Some info message"); log.log(logger::L_WARNING, "Warning message");
const int logger::L_ERROR = 1- corresponds to theerrorlog record typeconst int logger::L_WARNING = 2- corresponds to thewarninglog record typeconst int logger::L_INFO = 4- corresponds to theinfolog record type
-
record format: Default record format is
{"name":"${name}","thread":"${thread}","date":"${date}","type":"${type}","message":"${message}"}\n;${name}will be replaced with thenameparameter you provided to thelogger::Logconstructor${date}will be replaced with the date in ISO format in UTC timezome with microseconds (2020-05-30T09:53:15.535422Z)${thread}will be replaced with thestd::this_thread::get_id()${type}will be replaced with the corresponding log record type${message}will be replaced with the message to provided for the record
If you want to change the format you could inherit the
Logclass and change the correspondingformatvariable. There is a bunch of othervirtual privatemethods you may want to modify to adjust the logger for your preferences. -
One log file: If you would like to mix logs from different modules, you could define
Loginstance with the samepathparameter, so they both will use shared file stream to write log records.Example:
// module1.cpp logger::Log log("everything.log", "module1"); log.info("some module one information"); // module2.cpp logger::Log log("everything.log", "module2"); log.info("some another module information that will be pushed to the same file as module 1");
MIT
Have fun :)