POCO日志库使用

编程入门 行业动态 更新时间:2024-10-08 22:20:51

POCO<a href=https://www.elefans.com/category/jswz/34/1770796.html style=日志库使用"/>

POCO日志库使用

POCO是一个开源的C++类库,包含日志、多线程、网络等模块。
本文是对日志模块的简单使用。
1、LoggerHandle.h

#pragma once
#include "Poco/Logger.h"#include "Poco/AutoPtr.h" 
#include "Poco/Util/PropertyFileConfiguration.h"
#include "Poco/Util/LoggingConfigurator.h"extern Poco::Logger* _log;
Poco::Logger* initLoggerHandle();class LoggerHandle
{
public:LoggerHandle(void);virtual ~LoggerHandle(void);
};

2、LoggerHandle.cpp

#include "StdAfx.h"
#include "LoggerHandle.h"Poco::Logger* _log = initLoggerHandle();
Poco::Logger* p_log;
LoggerHandle::LoggerHandle(void)
{
}LoggerHandle::~LoggerHandle(void)
{
}Poco::Logger* initLoggerHandle()
{
    Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> pConf =  new Poco::Util::PropertyFileConfiguration("log_config.properties");    
    Poco::Util::LoggingConfigurator log_configurator;     log_configurator.configure(pConf);         p_log = &Poco::Logger::root(); return p_log;
}

以上代码是通过加载配置文件来进行配置,并且在其他文件中使用时只需要添加“LoggerHandle.h”这个头文件即可使用。

3、配置文件log_config.properties

#Configuring Formatters
#
#A formatter is configured using the "logging.formatters" property. Every 
#formatter has an internal name, which is only used for referring to it 
#during configuration time. This name becomes part of the property name.
#Every formatter has a mandatory "class" property, which specifies the actual
#class implementing the formatter. Any other properties are passed on to
#the formatter by calling its setProperty() method.
#
#A typical formatter definition looks as follows:
#     logging.formatters.f1.class = PatternFormatter
#     logging.formatters.f1.pattern = %s: [%p] %t
#     logging.formatters.f1.times = UTC
logging.formatters.f1.class = PatternFormatter  
logging.formatters.f1.pattern = [%Y-%m-%d %H:%M:%S] [%U(%u)] %s:%p:%t#Configuring Channels
#
# A channel is configured using the "logging.channels" property. Like with
# Formatters, every channel has an internal name, which is used during
# configuration only. The name becomes part of the property name.
# Every channel has a mandatory "class" property, which specifies the actual
# class implementing the channel. Any other properties are passed on to
# the formatter by calling its setProperty() method.
#
# For convenience, the "formatter" property of a channel is treated
# specifically. The "formatter" property can either be used to refer to
# an already defined formatter, or it can be used to specify an "inline"
# formatter definition. In either case, when a "formatter" property is
# present, the channel is automatically "wrapped" in a FormattingChannel
# object.
# 
# Similarly, a channel supports also a "pattern" property, which results
# in the automatic instantiation of a FormattingChannel object with a
# connected PatternFormatter.
# 
# Examples:
#     logging.channels.c1.class = ConsoleChannel
#     logging.channels.c1.formatter = f1
#     logging.channels.c2.class = FileChannel
#     logging.channels.c2.path = ${system.tempDir}/sample.log
#     logging.channels.c2.formatter.class = PatternFormatter
#     logging.channels.c2.formatter.pattern = %s: [%p] %t
#     logging.channels.c3.class = ConsoleChannel
#     logging.channels.c3.pattern = %s: [%p] %t#向控制台输出
logging.channels.c1.class = ConsoleChannel  
logging.channels.c1.formatter = f1  #向文件输出  
logging.channels.c2.class = FileChannel  
logging.channels.c2.formatter = f1 #The following properties are supported:
#       * path:         The log file's path.
#       * rotation:     The log file's rotation mode. See the 
#                       FileChannel class for details.
#       * archive:      The log file's archive mode. See the
#                       FileChannel class for details.
#       * times:        The log file's time mode. See the
#                       FileChannel class for details.
#       * compress:     Enable or disable compression of
#                       archived files. See the FileChannel class
#                       for details.
#       * purgeAge:     Maximum age of an archived log file before
#                       it is purged. See the FileChannel class for
#                       details.
#       * purgeCount:   Maximum number of archived log files before
#                       files are purged. See the FileChannel class
#                       for details.
#       * flush:        Specifies whether messages are immediately
#                       flushed to the log file. See the FileChannel class
#                       for details.
#       * rotateOnOpen: Specifies whether an existing log file should be 
#                       rotated and archived when the channel is opened.#path:The log file's path. 
logging.channels.c2.path = ./sample.log  #The log file's rotation mode.
#follwing values:
#           * never:         no log rotation
#           * [day,][hh]:mm: the file is rotated on specified day/time
#                   day - day is specified as long or short day name (Monday|Mon, Tuesday|Tue, ... );
#                          day can be omitted, in which case log is rotated every day
#                   hh  - valid hour range is 00-23;
#                         hour can be omitted, in which case log is rotated every hour
#                   mm  - valid minute range is 00-59;
#                         minute must be specified
#           * daily:         the file is rotated daily
#           * weekly:        the file is rotated every seven days
#           * monthly:       the file is rotated every 30 days
#           * <n> minutes:   the file is rotated every <n> minutes, 
#                   where <n> is an integer greater than zero.
#           * <n> hours:     the file is rotated every <n> hours, where
#                   <n> is an integer greater than zero.
#           * <n> days:      the file is rotated every <n> days, where
#                   <n> is an integer greater than zero.
#           * <n> weeks:     the file is rotated every <n> weeks, where
#                   <n> is an integer greater than zero.
#           * <n> months:    the file is rotated every <n> months, where
#                   <n> is an integer greater than zero and
#                   a month has 30 days.
#           * <n>:           the file is rotated when its size exceeds
#                   <n> bytes.
#           * <n> K:         the file is rotated when its size exceeds
#                   <n> Kilobytes.
#           * <n> M:         the file is rotated when its size exceeds
#                   <n> Megabytes.
logging.channels.c2.rotation = 50 M#The following values
#for the "archive" property are supported:
#
#           * number:     A number, starting with 0, is appended to
#                the name of archived log files. The newest
#                archived log file always has the number 0.
#                For example, if the log file is named
#                "access.log", and it fulfils the criteria
#                for rotation, the file is renamed to
#                "access.log.0". If a file named "access.log.0"
#                already exists, it is renamed to "access.log.1",
#                and so on.
#           * timestamp:  A timestamp is appended to the log file name.
#                For example, if the log file is named
#                "access.log", and it fulfils the criteria
#                for rotation, the file is renamed to
#                "access.log.20050802110300".
logging.channels.c2.archive = timestamp#Using the "times" property it is possible to specify
#time mode for the day/time based rotation. The following values
#for the "times" property are supported:
#
#           * utc:        Rotation strategy is based on UTC time (default).
#           * local:      Rotation strategy is based on local time.
#logging.channels.c2.times = utc#Archived log files can be compressed using the gzip compression
#method. Compressing can be controlled with the "compress"
#property. The following values for the "compress" property
#are supported:
#
#           * true:       Compress archived log files.
#           * false:      Do not compress archived log files.
logging.channels.c2press = false #Archived log files can be automatically purged, either if
#they reach a certain age, or if the number of archived
#log files reaches a given maximum number. This is 
#controlled by the purgeAge and purgeCount properties.
#
#The purgeAge property can have the following values:
#
#       * <n> [seconds]: the maximum age is <n> seconds.
#       * <n> minutes:   the maximum age is <n> minutes.
#       * <n> hours:     the maximum age is <n> hours.
#       * <n> days:      the maximum age is <n> days.
#       * <n> weeks:     the maximum age is <n> weeks.
#       * <n> months:    the maximum age is <n> months, where a month has 30 days.
logging.channels.c2.purgeAge = 30 days #The purgeCount property has an integer value that specifies the maximum number
#of archived log files. If the number is exceeded, archived log files are
#deleted, starting with the oldest. When "none" or empty string are
#supplied, they reset purgeCount to none (no purging). 
logging.channels.c2.purgeCount = 60  #The flush property specifies whether each log message is flushed
#immediately to the log file (which may hurt application performance,
#but ensures that everything is in the log in case of a system crash),
#or whether it's allowed to stay in the system's file buffer for some time. 
#Valid values are:
#
#       * true:  Every essages is immediately flushed to the log file (default).
#       * false: Messages are not immediately flushed to the log file.
#logging.channels.c2.flush = true  #The rotateOnOpen property specifies whether an existing log file should be 
#rotated (and archived) when the channel is opened. Valid values are:
#
#       * true:  The log file is rotated (and archived) when the channel is opened.
#       * false: Log messages will be appended to an existing log file,
#           if it exists (unless other conditions for a rotation are met). 
#           This is the default.
#logging.channels.c2.rotateOnOpen = false #空通道,发送到这个通道的message都不输出
logging.channels.c3.class = NullChannel 
logging.channels.c3.formatter = f1logging.channels.splitter.class = SplitterChannel  
logging.channels.splitter.channels = c1,c2,c3# Configuring Loggers
#
# A logger is configured using the "logging.loggers" property. Like with
# channels and formatters, every logger has an internal name, which, however,
# is only used to ensure the uniqueness of the property names. Note that this
# name is different from the logger's full name, which is used to access
# the logger at runtime.
# Every logger except the root logger has a mandatory "name" property which
# is used to specify the logger's full name.
# Furthermore, a "channel" property is supported, which can either refer
# to a named channel, or which can contain an inline channel definition.
#
# Examples:
#     logging.loggers.root.channel = c1
#     logging.loggers.root.level = warning
#     logging.loggers.l1.name = logger1
#     logging.loggers.l1.channel.class = ConsoleChannel
#     logging.loggers.l1.channel.pattern = %s: [%p] %t
#     logging.loggers.l1.level = information  
logging.loggers.root.channel = splitter  
logging.loggers.root.level = debug  

更多推荐

POCO日志库使用

本文发布于:2024-03-09 23:48:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1726470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   POCO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!