Basic Introduction to log4cplus

Source: Internet
Author: User

 Log4cplus has three main components:

    • Layouts
    • Appenders
    • Loggers

These three type of components found the basic of the log4cplus framework.

Here is a simplified UML Graph Modeling strating the relationships of those classes:

 

(1) A "layout" Class determines the format of the log message (How do we print out the message ).
And log4cplus comes with three layout class:

    • Simplelayout
    • Ttcclayout
    • Patternlayout

These classes accept log event and emits formatted text of messages, they work closely with appender.

(2) An appender writes Log message to somewhere (file, socket, etc ).

Appender functions like a callback.

When Log message is appended to a logger, The appender associated with that logger will be called.

Appender is the "Seam" for user to intecept the log message.

User can provide their own appenders to catch Log message, and this features is extremely useful when we need to keep an eye on the log message.

Log4cplus provides interface to help us do that.

As simple as it is, we only need to inherit from a base class: log4cplus: appender.

And implement several virtual function.

ClassMyappender:PublicLog4cplus: appender {Protected:Virtual VoidClose ();Virtual VoidAppend (ConstLog4cplus: spi: internalloggingevent &Event);}

 

Function close () will be called when the associated logger is shutdown.

Appender shoshould clean up its relative resource here.

Function append () is the callback that will be called when a log message is set to the logger (the logger that myappender is associated ).

The ParameterInternalloggingevent contains all the necessary information about an incoming "Log message ".

So, all you have to do is to get the message from the event, and formatted the message by layout class or by your own way.

Appender must have an associated layout class and an unique name. They have default values. However you can specify them by calling.

 

Appender-> setname ("Name") Appender-> Setlayout (layout );

 

 

OK, so far I have discussed how to creat an appender, next I will show you how to put it in the right position to work.

The key is: appender attached to at least one logger.

Logger keeps a list of appenders belong to it.

If you want to intercept message of some logger, just insert your appender to its appender list.

Myappender app; logger myloger; myloger. addappender (APP );

 

(3) a logger does the actually logging.

Logger has two main parts: appender, log level.

Appender is discussed above.

Log Level is for controlling which log message can be logged.

When a logger object is created, it starts life with a default appender of standard out and a default priority of none.

One or more appenders can be added to the list of destinations for logging. The priority of a logger can be set:

Not_set_log_level, trace_log_level, debug_log_level, info_log_level, warn_log_level, error_log_level, fatal_log_level

 

Those levels are in ascending order of "logginess" or importance level of each message.

FatalAndEmergAre two names for the same highest level of importance. Each message is logged to a logger object.

The logger object has a priority level.

The message itself also has a priority level as it wends its way to the log.

If the priority of the message is greater than, or equal to, the priority of the logger, then logging takes place,

Otherwise the message is ignored. The logger priorities are arranged such that:

Fatal_log_level> error_log_level> warn_log_level> info_log_level> debug_log_level> trace_log_level> not_set_log_level.

Not_set_log_levelIs the lowest and if a logger object is left withNotsetPriority,

It will accept and log any message.

Logging is done by logger.

Logger mylogger = log4cplus: logger: getinstance ("My Logger"); Mylogger. Log (level,"Message");

 

Logger is organized in hierarchy: logger belongs to a specific hierarchy.

Every hierarchy has root logger.

Logger has a parent (default is root logger ).

So all the loggers are linked in a tree-like structure.

Logger is not supposed to be created by user.
Log4cplus uses a factory pattern to create logger.

Logger: getinstance (ConstLog4cplus: tstring &Name, SPI: loggerfactory& Factory)

 

The code above help retrieve a logger with name "name". If the named

Logger already exists, then the existing instance will be returned.

Otherwise, a new instance is created.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.