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 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
# * minutes: the file is rotated every minutes,
# where is an integer greater than zero.
# * hours: the file is rotated every hours, where
# is an integer greater than zero.
# * days: the file is rotated every days, where
# is an integer greater than zero.
# * weeks: the file is rotated every weeks, where
# is an integer greater than zero.
# * months: the file is rotated every months, where
# is an integer greater than zero and
# a month has 30 days.
# * : the file is rotated when its size exceeds
# bytes.
# * K: the file is rotated when its size exceeds
# Kilobytes.
# * M: the file is rotated when its size exceeds
# 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.c2.compress = 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:
#
# * [seconds]: the maximum age is seconds.
# * minutes: the maximum age is minutes.
# * hours: the maximum age is hours.
# * days: the maximum age is days.
# * weeks: the maximum age is weeks.
# * months: the maximum age is 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 = f1
logging.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