当前位置:网站首页>Log4qt usage of logbase in QT project

Log4qt usage of logbase in QT project

2022-07-05 06:44:00 Suzhou frog

A log4qt As the log base of the project application layer , Just write down log4qt The use of .

1. Download and compile

Pull   https://github.com/devbean/log4qt On log4qt Code self compilation  icon-default.png?t=M5H6https://github.com/devbean/log4qt

2.log4qt The configuration file log4qt.conf

log4j.rootLogger=INFO,ROLLING_FILE
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.AppendFile=true
log4j.appender.ROLLING_FILE.MaxFileSize=10MB
log4j.appender.ROLLING_FILE.MaxBackupIndex=5
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] %m%n

Log parameter description , Reprinted from Log4Qt Basic use of _52_ Hertz's whale blog -CSDN Blog _log4qt

 One . The meaning of the parameter is explained 
 Type of output level 
ERROR、WARN、INFO、DEBUG
ERROR  For a serious mistake   It's mainly a program error 
WARN  For general warning , such as session The loss of 
INFO  For general information to be displayed , For example, log in and log out 
DEBUG  Debug information for the program 
 Configure log information output destination 
log4j.appender.appenderName = fully.qualified.name.of.appender.class
1.org.apache.log4j.ConsoleAppender( Console )
2.org.apache.log4j.FileAppender( file )
3.org.apache.log4j.DailyRollingFileAppender( Generate a log file every day )
4.org.apache.log4j.RollingFileAppender( A new file is generated when the file size reaches the specified size )
5.org.apache.log4j.WriterAppender( Send log information in stream format to any specified place )
 Configure the format of log information 
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
1.org.apache.log4j.HTMLLayout( With HTML Tabular layout ),
2.org.apache.log4j.PatternLayout( The layout mode can be specified flexibly ),
3.org.apache.log4j.SimpleLayout( Contains the level of log information and the information string ),
4.org.apache.log4j.TTCCLayout( Include the time when the log was generated 、 Threads 、 Categories and so on )
 Console options 
Threshold=DEBUG: Specifies the lowest level of output for log messages .
ImmediateFlush=true: The default value is true, It means that all messages will be output immediately .
Target=System.err: By default :System.out, Specify output console 
FileAppender  Options 
Threshold=DEBUF: Specifies the lowest level of output for log messages .
ImmediateFlush=true: The default value is true, It means that all messages will be output immediately .
File=mylog.txt: Specifies that the message is output to mylog.txt file .
Append=false: The default value is true, Add the message to the specified file ,false To overlay a message over a specified file content .
RollingFileAppender  Options 
Threshold=DEBUG: Specifies the lowest level of output for log messages .
ImmediateFlush=true: The default value is true, It means that all messages will be output immediately .
File=mylog.txt: Specifies that the message is output to mylog.txt file .
Append=false: The default value is true, Add the message to the specified file ,false To overlay a message over a specified file content .
MaxFileSize=100KB:  The suffix can be KB, MB  Or is it  GB.  When the log file reaches that size , It will scroll automatically , Move the original content to mylog.log.1 file .
MaxBackupIndex=2: Specifies the maximum number of scrolling files that can be generated .
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n
 The meaning of several symbols in log information format :
 -X Number : X Information output is left aligned ;
 %p:  Output log information priority , namely DEBUG,INFO,WARN,ERROR,FATAL,
 %d:  Output the date or time of the log time point , The default format is ISO8601, You can also specify the format after , such as :%d{yyy MMM dd HH:mm:ss,SSS}, The output is similar to :2002 year 10 month 18 Japan  22:10:28,921
 %r:  The output starts from the application to the output log The number of milliseconds that information takes 
 %c:  Category of output log information , Usually the full name of the class 
 %t:  Output the name of the thread that generated the log event 
 %l:  Where the output log event occurred , amount to %C.%M(%F:%L) The combination of , Including category name 、 The thread that happened , And the number of lines in the code . give an example :Testlog4.main (TestLog4.java:10)
 %x:  Outputs... Associated with the current thread NDC( Nested diagnostic environment ), Especially for things like java servlets In such a multi client and multi thread application .
 %%:  Output one "%" character 
 %F:  The name of the file where the output log message was generated 
 %L:  Line number in the output code 
 %m:  The message specified in the output code , Specific information of the generated log 
 %n:  Output a carriage return line feed ,Windows The platform is "\r\n",Unix The platform is "\n" Output log information line feed 
  Can be in % Add a modifier to the pattern character to control its minimum width 、 Maximum width 、 Alignment with text . Such as :
 1) c: Specify the output category The name of , The minimum width is 20, If category The name of is less than 20 Words , Right alignment by default .
 2)%-20c: Specify the output category The name of , The minimum width is 20, If category The name of is less than 20 Words ,"-" The number specifies left alignment .
 3)%.30c: Specify the output category The name of , The biggest width is 30, If category The name of is greater than 30 Words , It will cut off the extra characters on the left , But less than 30 There will be no spaces .
 4) .30c: If category The name of is less than 20 Just fill in the blanks , And right , If its name is longer than 30 character , Just truncate the characters that are output from the far left .

3. Project use

//path by log4qt Profile path 
Log4Qt::PropertyConfigurator::configure("log4qt.conf");

// stay Release Lower all qDebug()、qFatal() Wait for the debugging information output to the console to be written to the log file ,Debug Set not to write 
#ifdef QT_DEBUG
    Log4Qt::LogManager::setHandleQtMessages(false);
#else
    Log4Qt::LogManager::setHandleQtMessages(true);
#endif

//logger Multiple can be added Appender, So you can write the same message to multiple outputs “ file ” in , Only one... Is output here 
    QList<Log4Qt::Appender*> appenders = Log4Qt::Logger::rootLogger()->appenders();
    for(int i=0; i<appenders.size(); i++)
    {
        Log4Qt::Appender* appender = appenders[i];
        Log4Qt::FileAppender* fileAppender = dynamic_cast<Log4Qt::FileAppender*>(appender);
        if(fileAppender)
        {
            QString homePath = GetHomePath();
            QDir dir(homePath);
            if (!dir.exists()) {
              dir.mkpath(homePath);
            }
            fileAppender->setFile("path");
            fileAppender->activateOptions();
            break;
        }
    }

原网站

版权声明
本文为[Suzhou frog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050627231641.html