MyLog is a log-lib for Qt cross-platform applications. MyLog is stable and easy to use.
Read this in other languages: English, 简体中文
- Colorful text console output ;
- Support log levels of "info debug error";
- Multi-thread safe;
- Multi-threaded calls will not cause infinite memory growth;
- Support write log into a file, console and self-made-logger;
- Support self-implement logger;
- MyLog is an open-source project;
- Using MyLog is as easy as "qDebug()" and as powerfull as "qDebug()"(Cause MyLog is base on QDebug class);
- Could log(output) extra details, such as "level", "timestamp", "code file name" , "function name", "line number";
you should init MyLog before you log-out any string. e.g. Init the MyLog at the main function. Note: Don't start a thread which using "MyLog" to log-out at main function directly
1.Include the lib or src at your xxx.pro
#using MyLog as the src (using your own path)
#include($$PWD/my_lib/MyLog/MyLogSrc.pri)
#using MyLog as a lib (using your own path)
include($$PWD/my_lib/MyLog/MyLogLib.pri)
- Include header
#include "my_log.h"
- Init for file log
QCoreApplication::setApplicationName("myappname");
QCoreApplication::setApplicationVersion("0.0.1");
QCoreApplication::setOrganizationName("com.company.myappname"); //set app name for log-file-path
QCoreApplication::setOrganizationDomain("com.company.myappname");
MyLogNS::FileLogger *fileLog = new MyLogNS::FileLogger();
int result = fileLog->open_log_file_at_dir("log");
if(result != 0) {
qDebug("error: %s", fileLog->get_error_str(result));
}
qDebug("log file path=%s", fileLog->get_log_file_abs_path());
MyLogIns.installer_logger(fileLog);
- Init for console log
MyLogIns.installer_logger(new MyLogNS::ConsoleLogger());
- Features control variables using the variables when you need change the features only.
MyLogIns.is_enable_auto_new_line= true; //default is true
MyLogIns.is_show_level_str= true; //default is true
MyLogIns.is_show_timestamp= true; //default is true
MyLogIns.is_show_file_name= true; //default is false
MyLogIns.is_show_function_name= true; //default is true
MyLogIns.is_show_line_number= true; //default is true
- Write log (use it as the way using the "qDebug()")
I << "str value=" << 1; //log info
D << "str value=" << 2; //log debug
E << "str value=" << 3; //log error
D << "Date:" << QDate::currentDate();
D << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);
- Switch about each level of log you can find the macro at “MyLogSrc.pri” or “MyLogLib.pri”
#DEFINES += MYLOG_NO_I_OUTPUT
#DEFINES += MYLOG_NO_D_OUTPUT
#DEFINES += MYLOG_NO_E_OUTPUT
- Log example on console
- Method of making a self-implemented logger inherit this interface and make your own logger. you can find this inferface at file "logger_interface.h"
class LoggerInterface
{
public:
LoggerInterface();
virtual ~LoggerInterface();
public:
virtual void open() = 0;
virtual void close() = 0;
virtual void write(LogLevel level, const QString &msg, bool is_shift_to_next_line) = 0;
};
- Create your own logger
#include "logger_interface.h"
class NetLogger : public MyLogNS::LoggerInterface
{
public:
NetLogger();
virtual void open();
virtual void close();
virtual void write(MyLogNS::LogLevel level, const QString &msg, bool is_shift_to_next_line);
};
- Using your own logger
MyLogIns.installer_logger(new NetLogger());