Log4 C Developers Guide

download Log4 C Developers Guide

If you can't read please download the document

Transcript of Log4 C Developers Guide

Log4C Developers Guide

1.0.6 13 Nov 2006First Draft: pulls together information from README files and various 'how to ' documents for Log4C. This document is started between the release of Log4C 1.1.0 and 1.2 so although much of it is valid for older releases it is more looking towards the 1.2 release. Where you see xxx in the text this denotes an item we need to get back to.This document was created with Open Office 2.0.2 .

Table of Contents1 Introduction22 Log4C Concepts and Overview33 How to download and build Log4C43.1 Parsing the Log4C configuration file--the expat dependency43.2 Some comments on building Log4C in the corporate factory53.2.1 Log4C is released under the LGPL license. 53.2.2 Building on different platforms53.2.2.1 Building On Unix53.2.2.2 Building on Windows63.2.2.3 Building on MAC OS63.2.3 Packaging on different platforms64 Getting started--running the examples64.1 Running application_174.2 Configuring the priority for a category84.3 Configuring application_1 to log to a set of log files94.4 What about application_2 ?105 Using the Log4C API105.1 The helloworld example with Log4C105.1.1 The helloworld code105.1.2 Configuring and running helloworld115.1.2.1 Configuring helloworld with the configuration file125.1.2.2 Configuring helloworld with the environment variables125.1.2.3 Configuring helloworld via the Log4C API135.2 Comments on the the different configuration methods135.3 Interaction of the configuration methods145.4 Including Log4C calls into your code 145.4.1 Making it easy to remove Log4C calls from your code145.4.2 Other useful Log4C functions165.4.3 Multi threaded applications using Log4C175.4.3.1 Why the Log4C API is not fully MT safe175.4.3.2 How to use Log4C in an MT safe way186 Extending Log4C with your own custom appender and layout types187 Troubleshooting and debugging218 Deployment Experience with Log4C218.1 Blame it on the logger218.2 Preparing to move logging from development into a production
environment.229 References24

Introduction

Log4C is a C-based logging library. Its goal is provide the C software developer with a configurable and efficient library for logging messages. So Log4C provides the classic logging functionality required by most projects by default while leaving the scope for extensibility for those projects that have special requirements.

It seems extraordinary that just about every C project ends up developing it's own logging system. While there are environments like embedded systems or operating system kernels that may justify customized logging technology (and there are open source projects aimed at those areas) there seems to be no a priori reason why most user level application and server software written in C cannot share a logging library.

Log4C may be linked with end user applications or other libraries that will eventually be linked into applications. The library is released on SourceForge under the LGPL license.

This document is aimed at developers who wish to use Log4C to manage logging in their own project.The kind of information you can find here includes:

a general introduction to Log4C concepts and how Log4C can be useful to you

how to download and build Log4C

how to use and configure the library via examples

how to extend Log4C with your own code for customizing, for example, where messages are logged and how they are formatted.

troubleshooting and debug information

deployment experience with Log4C

references to useful material

For information relating to Log4C internals the intention is to create a separate documents, Log4C Internals. This will help keep the size of each document reasonable and help decouple the evolution of those documents.Log4C Concepts and Overview

Log4C follows the overall style of the native Java logging and Log4j utilities. There are three fundamental types of object in Log4C: categories, appenders and layouts.

You can think of these objects as corresponding to the what, where and how of the logging system: categories describe what sub-system the message relates to, appenders determine where the message goes and layouts determine how the message is formatted.

So for example a category might be named httpd.network.connection that may be logging messages to the syslog appender using the dated format. The convention of using dotted names for categories determines an obvious hierarchy which is used to determine parent and child relationships during logging. These are used because during logging (and this is configurable) a message logged by a category will also be logged to the appenders of any parent categories.

There is always a root category which represents a conceptual root of the hierarchy of categories.

The categorization of the system make sit easy to select which messages are logged and whereif I configure an appender and priority for a given category then I automatically do it for all it's descendants (unless I explicitly say not to). The extreme case of this is the root categoryany configuration positioned there will by default apply to all categories in the system. However, if I need to drill down for finer grained control then the categorization allows me to do that.

This means that you should expect to spend some time defining the categories within your system. As an example, if your system separates into different applications then you should have at least one category per application. If your product ships a set of libraries, then each library probably merits it's own category. Libraries linked in as a dependency on a given library should probably be sub-categories of the including library. Within a library you might identify other sub categories, for example a housekeeping category, a connection manager category, a request processor and so on that correspond to logging messages that will come from those sections of code.

The other concept fundamental to Log4C is the logging priority: each message has an associated priority. At a given point a given instance of Log4c has an associated configured priority--only messages with a higher priority than the current Log4c priority are sent to an appender for processing. How to download and build Log4C

The README file shipped in the workspace gives a good account of Log4C dependencies and how to do a basic build so we will not repeat all that information here. Here we treat that information that requires more detail than the basic build instructions.

Parsing the Log4C configuration file--the expat dependency

The expat dependency--no it's not the latest Robert Ludlum novel...it refers to the fact that Log4C can use the expat library (cf. http://expat.sourceforge.net/) as the low level xml parser for it's configuration file. By default when you run configure, it will look for expat and if it finds it it will use it. You can tell configure not to go looking for expat by using the --without-expat option.

In the case where you have specified --without-expat or expat is not found or is the wrong version or fails to compile with a small test program, then Log4C will use a custom XML parser using lex/yacc code for the lower level xml parsing. This code works ok with the current configuration file and bundled appenders and layouts.

So why do you need to know how Log4C handles it's xml parsing ? Well, as a developer integrating Log4C you will probably prefer the bundled parser option as it means you do not pull a dependency on expat. However from the point of view of maintaining Log4C there is still a dependency on the lex/yacc tools needed to generate the parsing code. So there is a choice to make in terms of which dependency Log4C manages. The current consensus is that expat is now sufficiently widespread and mature that introducing a dependency on it is a reasonable step.

So as a developer consuming Log4C you can rely on the older bundled code staying in Log4C for the foreseeable future. But you should not expect bug fixes therein nor that that code will evolve to take account of potential enhancements to the configuration file. So the message is that you should start to prepare for handling the dependency on the expat library, either by bundling it in your delivery with Log4C or ensuring that it is available natively on your supported platforms or by specifying it as a requirement to your end users.

Some comments on building Log4C in the corporate factory

The Log4C source code is free, which is good, but there is still a cost in integrating it into your project. Here are some points to consider on this. Log4C is released under the LGPL license.

See the COPYING file in the release for the full license but this means a few things:you will almost certainly require a legal review inside your company to approve your use of Log4C.

you are required by the LGL license to ship along with your project a copy of the Log4C source as used in your project. You are also required to ship a copy of the LGL license. Typically one ships a file called something like, ThirdPartyLicenses which lists all the open source components used by your project and includes all the required licenses for those open source components.

Ideally the changes you make to Log4C will be minor porting, bug fixes or integration issues required to integrate with your project. Ideally, you would invest some time to work with the Log4C maintainers to integrate your changes back into the Log4C repository in a way that was useful to the whole community. Thus, while one might go with some 'quick fixes' in order to integrate Log4C, expect to spend some time refactoring or generalizing the code in order to have it accepted back into the public repository.A general aspect of integrating an open source project is that, for various reasons, you may wish to make it easy to remove the dependency. We discuss this in more detail in the section titled Integrating Log4C calls into your code. Building on different platforms Building On Unix

On Unix systems (certainly for Linux, Solaris and HP-UX) you should be able to use the provided autoconf tools to build Log4C as described in the README. One tip for managing different platforms is not to compile the code in the source directories, but rather to create a directory per platform, run the configure script therein to generate the Makefiles and then build the library, tests, examples and doc in per-platform directory. This allows you to use the same source code to maintain binaries for multiple platforms at the same time. So an example compile session might run:

On my Solaris x86 build machine:[mySolarisx86BuildMachine ]$ tar -zxvf log4c-1.0.12.tar.gz[mySolarisx86BuildMachine ]$ mkdir build-Solaris-x86; cd build-Solaris-x86[mySolarisx86BuildMachine ]$ ../log4c-1.0.12/configure --prefix=/path/of/installation[mySolarisx86BuildMachine ]$ make

then on the Solaris sparc build machine:[mySolarisSparcBuildMachine ]$ mkdir build-Solaris-sparc; cd build-Solaris-sparc[mySolarisSparcBuildMachine ]$ ../log4c-1.0.12/configure -prefix= path/of/installation[mySolarisSparcBuildMachine ]$ make

and so on.If you need to use a compile other than gcc then use the CC environment variable as descirbed in the README file. Building on Windows

The build for Windows is not yet integrated into the autoconf mechanism (due to limitations in autoconf on windows at the time the integration was done). So a makefile is provided in the msvc6 directory for building on Windows.

Note that some of the appenders are not compiled on Windows...for example the syslog appender.See the README in the msvc6 directory for the Windows build details. Building on MAC OS

There are some project files provided for compiling on MAC OS. xxx Not sure of the status of the mac stuff at this time. Packaging on different platforms

Using the provided makefilery as explained in the section Building on different platforms it should be possible to generate the library, tests and examples. However native packaging is provided only for Linux at this time. Even there you will probably wish to customize the naming and so on of the generated rpms.

So you should expect to spend some time creating scripts or makefiles that will take the binaries from a build directory and package them as required for your supported platforms.Another solution is just to bundle the Log4C library in one of your existing packages or rpms. Getting started--running the examples

Once you have built Log4C you can go into the examples directory and run the examples. There may be additional information in the README in the examples directory.

For ease of presentation the text below tends to suppose a Unix (or cygwin) environment. We don' t translate each command to Windows supposing that the translation of commands, pathnames and so on are fairly obvious. However where there are significant differences for Windows then we try to highlight them. Running application_1

If you have built in a directory separate from the sources, as is recommended, then you need to copy the log4crc file from the source directory into the build directory:$ cd examples$ cp ../log4c/examples/log4crc .

Then run the example application_1 as follows. You will need to put the Log4C library on some path so the system can find it when application_1 starts up. If you have used the autoconf tools on Unix and the Makefile in the msvc6 directory that will look like this:On Linux/Solaris: export LD_LIBRARY_PATH=../src/log4c/.libs/On HP-UX: export SHLIB_PATH=../src/log4c/.libs/On Windows: set Path=..\log4c;%Path%

Then you can run application_1 as follows:

$ ./application_1 4

You should see this:

[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 0[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 0[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1 ../../log4c/examples/application_1.c

application_1 is a simple example that loops, logging two messages on each loop. In the output above we only see the error message logged at priority ERROR, the other message logged at priority DEBUG is not logged with the default configuration in the log4crc file.

But why is the log message being logged twice ?

Thank you for asking. What's happening is that although the application submits the message only once to Log4C, it is passed to all the appenders attached to the originating category six13log.log.app.application1 AND it's ancestor categories. So, if you look in log4crc you will see this line:

So the stdout appender for the category six13log.log.app.application1 is logging the message and the stdout appender for the ancestor category six13log.log is also logging it.

So why this strange behavior with respect to logging messages to multiple appenders ? Well, in this example it's hard to see the point. But suppose we have multiple applications and associate categories six13log.log.app.application2, six13log.log.app.application3, six13log.log.app.application4 , say, with them. Then I can get very tight control over what gets logged using the categorization of the messages: if I set an appender on six13log.log.app for example then I will get the messages from all the applications. But if I am just interested in messages from a subset of categories then I can configure an appender and logging priority just for them.

So once the application or system designer has done a good job defining the categories available for logging then Log4C offers the administrator or end user very powerful and easy control over the messages he chooses to log, where they get logged and the format they are logged in. Furthermore, even if the application developer has to go back and introduce new categories then with Log4C that's an easy task. Too often in-house logging mechanisms are hard coded for a specific domain and adding what would amount to new categories requires significant additional coding. Not so with Log4C.

You can see that there is additional text being logged in addition to the message from the application. This 'overhead' is added by Log4C and the various parts of the message break down like this:

[stdout] The name of the appender logging the message surrounded by square brackets.ERROR The log level of the message. six13log.log.app.application1 The name of the catgory originating the messagesome error from app1 at line 78 in file ../../log4c/examples/application_1.c The actual log message. Configuring the priority for a categoryNow, edit the log4crc file to change the value of the priority field from error to debug:

Run application_1 again, you should see this:

[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 0[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 0[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 0[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 0[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 1[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 1[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1

What's happening now is that because the priority of the category has been lowered to DEBUG, the debug message in the application is now appearing. And, for the reasons explained above for the ERROR message, it appears twice. Configuring application_1 to log to a set of log filesLogging to stdout is fine but suppose we wish to trap all the messages for six13log.log.app.application1 to a set of log files. To do this we can use the rolling file appender.

Edit this line in log4crc to replace stdout with myrollingfileappender:

Run application_1 again. This time on stdout you will see:[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 0[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 0[stdout] DEBUG six13log.log.app.application1 - Debugging app 1 - loop 1[stdout] ERROR six13log.log.app.application1 - some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1

The other copies of the log messages are no longer being sent to stdout but rather to the myrollingfileappender appender, which is logging them to a set of log files in the current directory of the form myprefix.*. Verify this by dumping one of the log files:$ cat myprefix.0 20061105 16:29:33.530 DEBUG six13log.log.app.application1- Debugging app 1 - loop 0 20061105 16:29:33.604 ERROR six13log.log.app.application1- some error from app1 at line 79in file ../../log4c/examples/application_1.c - loop 0 20061105 16:29:36.605 DEBUG six13log.log.app.application1- Debugging app 1 - loop 1 20061105 16:29:36.605 ERROR six13log.log.app.application1- some error from app1 at line 79 in file ../../log4c/examples/application_1.c - loop 1

You will notice that these log messages also have a date stamp on them. This is because myrollingfileappender has been configured in log4crc to use the dated layout (rather than the basic layout) and this layout inserts a date stamp in the message before logging it.

You will also notice that the unlike the stream appender which is logging to stdout, the rollingfile appender does not prefix it's messages with the name of the appender enclosed in square brackets. The reason for the change is twofold: first we did not see a real need to log the appender name in each message and secondly it seems better from a design point of view if it is only the layout code which determines the format of the message. What about application_2 ?As an application application_2 does the same thing as application_1it loops logging a few messages. However the interesting thing about application_2 is that it links with a library where new appenders and layouts have been defined. So we leave this more advanced topic to a later section. Using the Log4C APIIn the last section we saw how to control the logging output from a Log4C enabled example using the configuration file. In this section we see how to Log4C enable your applications using the Log4C API.

At the top of the main Log4C home page you will see links to the API documentation for the latest released Log4C, for example here: http://log4c.sourceforge.net/files.html. You should keep a reference to this documentation when working with Log4Cit documents parameters, call conventions, return codes, issues and sometimes has code snippets. If you have checked out the workspace from CVS you will need to have added enable-doc to build the documentation, which gets put into the doc directory where you build. You are strongly recommended to build and view (and improve!!) the API documentation.

Depending on the utilities available (dyoxygen, latex,...) on your system the API documentation can be generated in html, pdf and man page format. See 'configure help' and the README for details on this. The helloworld example with Log4C

As tradition dictates we provide a hello world example to illustrate the bare bones of Log4C. The hello world code looks as follows: The helloworld code

#include #include "log4c.h"int main(int argc, char** argv){int rc = 0;log4c_category_t* mycat = NULL;if (log4c_init()){printf("log4c_init() failed");rc = 1; }else{mycat = log4c_category_get("log4c.examples.helloworld");log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Hello World!");/* Explicitly call the log4c cleanup routine */if ( log4c_fini()){printf("log4c_fini() failed");}}return 0;}As you can see there are four calls to the Log4C API here. The log4c_init() call initializes the library, and reads the configuration files (if any) and environment variables. We will see more about the configuration mechanisms below.

The log4c_category_get() function returns a reference to a category required for logging.

The log4c_category_log() function logs the age old message .

The log4c_fini() tells Log4C to cleanup, freeing memory, closing files and so on.

Configuring and running helloworld

See the section Running application_1 for information on setting the environment before running helloworld. If you have compiled and run the helloworld example as is you will see...no output :-(.

This is because although we have done all the right things, we have not as yet configured an appender for our helloworld category. It will be instructive to visit each of the three ways Log4C provides to configure it's behavior.

For information, the command line on Linux to compile a Log4C example, supposing that the log4c library is already compiled, looks like this:$ gcc -I ../../log4c/src -I ../src ../../log4c/examples/helloworld/helloworld.c -L ../src/log4c/.libs/ -llog4c Configuring helloworld with the configuration fileCopy the log4crc file into the helloworld directory and edit it to add this line:

For completeness we cite here the two other lines relevant to the configuration for helloworld, which are already in the file:

Run the helloworld program again. You will now see:[stdout] ERROR log4c.examples.helloworld - Hello World!

Cool. What has happened is that we have configured Log4C to send messages for our helloworld example, logged with priority DEBUG or above to go to the appender called stdout. This appender is a stream based appender with name stdout, which means the messages will go to...well, stdout. The appender called stdout also specifies it's layout, which is the basic layout type. Of course we could configure the messages to go to any of the existing Log4C appenders and use any of the layouts to format the message.

By way of building a mental model of what is happening inside the Log4C library note that the fact that the helloworld category is mentioned in the configuration file means that after the call to log4c_init(), a nicely configured instance of that category already exists inside the library. In the case where that category is not mentioned in the configuration file then it is the call to log4c_category_get("log4c.examples.helloworld") that creates the (unconfigured) instance on this category. Configuring helloworld with the environment variablesLog4C provides a minimal configuration ability via two environment variables: LOG4C_APPENDER and LOG4C_PRIORITY. Both these variables act only on the root category but provide a quick way to globally enable logging . This can be useful to take a look at log messages before drilling down into a finer grained logging using the categorization to focus on messages of particular interest.

So, remove the line from the configuration file you added above:

Rerun helloworld to confirm you have no output then rerun the program as follows:export LOG4C_APPENDER=stdout; helloworld

You will now see the log message as before:[stdout] ERROR log4c.examples.helloworld - Hello World!

What is happening here is that at init time Log4C is reading the LOG4C_APPENDER environment variable and configuring the stdout appender for the root category as a result. Thus all messages logged to this instance of the library, at a priority higher than NOTICE (for that is the priority set on the root category in the configuration file) will be sent to stdout.We could just as well have set LOG4C_APPENDER to be myrollingfileappender, as defined in the configuration file, in which case the helloworld message would have been logged to the myprefix.0 file. Go on, give it a lash, it works! Configuring helloworld via the Log4C APIThe configuration achieved in the above sections via the configuration file or the environment variables can also be done manually using the Log4C API. Add the following call to set the appender for the helloworld category:mycat = log4c_category_get("log4c.examples.helloworld");log4c_category_set_appender(mycat, log4c_appender_get("stdout")); /* ADD THIS LINE */log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "Hello World!");

Recompile and rerun helloworld to see the output:[stdout] ERROR log4c.examples.helloworld - Hello World! Comments on the the different configuration methodsThe three configuration methods demonstrated above are likely to be of more or less interest depending on the nature of your project. Far be it from Log4C developers to adopt the arms dealer pattern of technology development, so here are some general observations to help you think about which method or methods are most appropriate for your scenario:

The environment variables seem to be generally useful as a quick way to get your application or system outputting messages.

The configuration file is certainly a very powerful way and easy way to configure logging. It is probably of most utility in those cases where you are quite close to the final application development as this gives you more say in the nature of the configuration requirements you introduce to application users.

Using the API to configure Log4C is probably of use to those projects delivering some intermediate technology or framework to other application developers. This is because as a framework or library provider you probably do not wish to contribute to a kind of configuration file proliferation. You probably would rather take configuration from your own configuration files, or even programmatically from your application developers and then configure Log4C programmatically. If you use the Log4C configuration file and your framework has it's own configuration file and your application has it's own configuration file then it's getting a bit hard for your administrator to keep track of all these configuration files.

Interaction of the configuration methods

The log4c_init() function looks for a configuration file first and then reads the environment variables, so for defining the priority and appender for the root category the environment variables take precedence. The order that Log4C (up to version 1.0.12) searches for the configuration file is $LOG4C_RCPATH/log4crc", "$HOME/.log4crc", "./log4crc" . This seems wrong and will probably be changed or made configurable. Also, it is not clear if there is a well defined design decision about what happens when loading fails in a file or if, once a file is loaded successfully, we should then stop trying to load other files. xxx revisit this on the list.If you configure Log4C objects after the call to log4c_init() then the new settings you configure take precedence. Including Log4C calls into your code Making it easy to remove Log4C calls from your code

For various reasons you may wish to wrap calls to the Log4C API either in macros or function calls. We list some of these here to help you decide if you want to use wrapped or naked calls to Log4C.You may wish to do this in order, for example, to do some generic preparation of your logging messages.

Another reason might be to define some smaller subset of the Log4C API that you wish all your library writers or application developers to useby using the interface you define you can ensure a consistent usage of Log4C across your system.

Another reason might be that, Log4C being an external open source project, you may wish to make it easy to remove Log4C from your project, for fear of API stability, quality or licensing issues that may arise later in your project.

As an example, we present one way of wrapping the Log4C calls here. We will modify the helloworld.c file to remove the hard coded dependency on Log4C and introduce a level of indirection that allows an easy decoupling of helloworld from Log4C.

We simply introduce functions that wrap the required Log4C functions: mylog_init() for log4c_init(), mylog_fini() for log4c_fini() and so on. We also need to define our own copies of the logging priorities. See the file mylog.h for the full definitions. So, for logging single strings we can use a function like this:static LOG4C_INLINE void mylog_msg(char *catName,int a_priority, char *msg){#ifndef WITHOUT_LOG4Clog4c_category_log(log4c_category_get(catName), a_priority, msg);#elseprintf(msg);#endif }You can see that if this code is compiled with -DWITHOUT_LOG4C then Log4C is totally removed.

To use this code, rather than directly calling log4c_category_log() we would do this:mylog_msg("log4c.examples.helloworld", MYLOG_PRIORITY_ERROR, "Hello World!");

One suggestion by this writer http://jefficus.usask.ca/article.php?story=20040613140849924 is to define a macro for the logging call which means you need only mention hte category name for your subsystem in one place, typically in some include file used by all files in that subsytem.

So your simplified logging call actually looks like this:MYLOG_MSG(MYLOG_PRIORITY_ERROR, "Hello World!");

but at the start of the helloworld.c file you define the category name to use, which is picked up by the MYLOG_MSG macro:#define MYLOG_CATEGORY_NAME "log4c.examples.helloworld"

The MYLOG_MSG macro is defined such that if you do not define a category name then it defaults to the root category. This means it is safe and, if you want to be lazy, easy to get logging.

The only comment on this handy technique is that if you are paranoid about performance then you may not want to have the lookup call based on the logger name at each logging call. In this case you would do the call to log4c_category_get() (or the wrapped equivalent) at the initialize time of your subsystem and then use the actual reference to the category object rather than the category name.

The code to wrap the log call to take a variable number of arguments is a bit more complex than the code used to log one string shown heresee the function mylog_log() defined in mylog.h for way to do that. The code discussed above is included in helloworld1.c and the file mylog.h. The full code of helloworld1.c is included here for convenience:

#include #define MYLOG_CATEGORY_NAME "log4c.examples.helloworld"#include "mylog.h"int main(int argc, char** argv){int rc = 0;if (mylog_init()){printf("mylog_init() failed");rc = 1; }else{MYLOGMSG(LOG4C_PRIORITY_ERROR, "Hello World!");/* Explicitly call the log4c cleanup routine */if ( mylog_fini()){printf("mylog_fini() failed");rc = 1;}}return rc;} Other useful Log4C functions

See the Log4C API documentation in your workspace (as generated by make if you have done 'configure enable-doc') or here for the last release: http://log4c.sourceforge.net/files.html.

Log4C provides some convenience functions that mean you do not have to specify the logging priority in the logging callfor example log4c_category_fatal(), log4c_category_error() and so on. These are documented here for the last release: http://log4c.sourceforge.net/category_8h.html.

You can test to see the logging priority of a given category using the log4c_category_is_priority_enabled() function.

Browse the documentation and examples and tests source to see other usage of the Log4C API. Multi threaded applications using Log4C

Log4C is not Multi thread safe (MT safe). xxx This topic should be discussed on the mailing list and revisited systematically to see what can be done and to document the MT status of the various API calls.

In the meantime, it is possible to use Log4C safely provided you follow some rules related to initialization and provided the appenders and layouts you use are themselves MT safe. Why the Log4C API is not fully MT safe

A fully MT safe library would be such that multiple threads in the application could safely call any of the LOG4C API functions without any need for synchronization on the part of the application. There are several places where Log4C does not satisfy this:log4c_init() This function must be called successfully just once by the application. If two threads call this at the same time the behavior is undefined. The problem is that both threads will start to initialize global objects and the behavior is this case is undefined.

log4c_fini() The same is true of log4c_fini()this function can safely be called by only one thread at a time. Furthermore when you call log4c_fini() you must be sure that there are no calls to the logging API in flightif there are then log4c_fini() goes ahead freeing up Log4C objects (like categories, appenders and layouts) so this will probably crash your application.

Readonly type Log4C calls like log4c_category_is_priority_enabled() and so on are MT safe between themselves and calls to logging functions like log4c_category_log() but are not MT safe with respect to log4c_init() and log4c_fini().

Issues related to lazy creation and intialization of objects.

Lookup and creation type calls like log4c_category_get(), log4c_appender_get() , log4c_layout_get() and so on are not MT safe, even between themselves. The problem is that if two threads call log4c_category_get() at the same time then, if the category instances do not yet exist in the internal store of category instances, then the threads may clash as they write to the shared hash that stores the instance references. This is because no synchronization is provided on the shared instance hash.

Calls to log4c_category_log() are not MT safe when called on the same categories. The problem is that log4c_category_log() eventually calls log4c_appender_append() to log the message. However, if the appender has not already been opened then it calls log4c_appender_open(), which is not MT safe. The result could be that the two threads will to open the appender at the same time which will give rise to undefined behavior.

The calls to the currently shipped basic and dated layouts all work in the context of a logging event object which is allocated by the log4c_category_log() call on the stack. Thus the current usage of the layout interface is in fact MT-safe. How to use Log4C in an MT safe way

It can be seen from the last section that if you can ensure the following two conditions then you should be able to use Log4C safely in an MT environment:

perform the initialization of Log4C and the creation and intialization of all the log4C logging objects before before starting up the multiple threads that call Log4C.

In reality this can be problematic as in order to ensure that open has been called on all the appenders in your system you will need to know the appender names up front at init time. For developers configuring Log4C programatically then this may workby definition they will have the names of all the Log4C objects available, otherwise they could not configure then programmatically. For those using the Log4C configuration file where the Log4C init time code takes care of the creation of the objects there is currently no way to tell that code call open at init time. There seem to be a few possibilities here that require some development work in Log4C xxx:A way to do that could be to provide an option 'call open at init time' in the configuration file.

Provide a function like log4c_appender_open_all() that would call open on all appenders in Log4C. This way the application writer could call that routine to explicitly open all the appenders and avoid race conditions as various threads try to open them.

A third solution would be that log4c guarantees that the open call on an appender is called only once in a critical section.

Ensure that there are no logging calls in flight when log4c_fini() is called.

Extending Log4C with your own custom appender and layout types

The application_2 program in the examples shows how to define your own appender and layout types.

First a short word on Log4C terminology. The Log4C library ships with a set of base types. These base types are category, appender, layout and rollingpolicy. These base types are hard coded into the librarythere is no run time way to control the creation or deletion of these base types (which perhaps there should be). The base types all define an interface which one can use to create new types from the base type. For example the stream appender, the stream2 appender and the rollingfile appender are all types of appender. The basic and dated layouts are both type of layout. The only rollingpolicy currently supplied with Log4C is sizewin, which is a type of rollingpolicy.

Once a base type exists in Log4C it is very easy to add new typesthe application_2 example shows how to do this for appenders and layouts. Of course it is possible to add new base types to Log4C but we leave that more as a topic for the Log4C Internals document as it is less common to need to do that.

Note that it is not possible to extend the set of priorities defined by Log4C. These follow the syslog(3) model and are defined as follows: typedef enum { /** fatal */ LOG4C_PRIORITY_FATAL = 000, /** alert */ LOG4C_PRIORITY_ALERT = 100, /** crit */ LOG4C_PRIORITY_CRIT = 200, /** error */ LOG4C_PRIORITY_ERROR = 300, /** warn */ LOG4C_PRIORITY_WARN = 400, /** notice */ LOG4C_PRIORITY_NOTICE = 500, /** info */ LOG4C_PRIORITY_INFO = 600, /** debug */ LOG4C_PRIORITY_DEBUG = 700, /** trace */ LOG4C_PRIORITY_TRACE = 800, /** notset */ LOG4C_PRIORITY_NOTSET = 900, /** unknown */ LOG4C_PRIORITY_UNKNOWN = 1000} log4c_priority_level_t;

So, turning to application_2...if you look at application_2.c you will see that it is very similar to application_1.cit loops logging two messages at each loop. However, it includes an additional call before the call to log4c_init():init_examples_lib();

This function is defining some new appender and layout types that can be accessed in the configuration file or programatically. It eventually calls init_example_appenders(). A key code snippet from that file looks like this:

static const log4c_appender_type_t * const appender_types[] = {#ifdef HAVE_SYSLOG_H&log4c_appender_type_syslog_local0,&log4c_appender_type_syslog_user,#endif &log4c_appender_type_s13_file,&log4c_appender_type_s13_stderr};static size_t nappender_types = sizeof(appender_types) / sizeof(appender_types[0]);int init_example_appenders(){int rc = 0; int i = 0;for (i = 0; i < nappender_types; i++) log4c_appender_type_set(appender_types[i]); return(rc); }

The key call is to the (somewhat badly named) log4c_appender_type_set() function. This function is registering the appender types defined here with the Log4C code. Thereafter they are available for use in the configuration file or accessible programmatically.

We list here the complete code fragment that deals with the log4c_appender_type_s13_file type:/*******************************************************************************//** The log4c stderr adds a "[stderr] " in front of the message* it is logging. this one doesn't, and leaves the formatting* descisions up to the formatter*/static int s13_stderr_append(log4c_appender_t* this, const log4c_logging_event_t* a_event){return fprintf(stderr, "%s\n", a_event->evt_rendered_msg);}static int s13_stderr_open(log4c_appender_t* this){/* fprintf (stderr,"running s13_stderr appender open command now\n"); */return 0;}const log4c_appender_type_t log4c_appender_type_s13_stderr = {"s13_stderr",s13_stderr_open,13_stderr_append,NULL,};/*******************************************************************************/The log4c_appender_type_s13_stderr object is defining the interface that the core Log4C code will use to open, append and close this appender. As you can see this appender is pretty simple. It's close function is NULLit is safe to do this the core Log4C code will not try to call a NULL function. The name of this appender is defined in the first field of it's interface object and is s13_stderr". It is this name that can be used either in the configuration file or programatically in the log4c_appender_get() call to obtain a reference to the actual object.

Defining new layout types follows a similar pattern. See the example_formatters.c file for some example code. Defining new rollingpolicy types--which are used to tell rollingfile appenders under which conditions rollover events should occurproceeds in the same way. Other types of rollingpolicy it would be interesting to add would be a policy that triggered rollover based on a time period or fixed time basis. Troubleshooting and debuggingIf you have run configure with the enable-debug option then by setting the environment variables SD_DEBUG and SD_ERROR to the value 1 you should see some useful internal Log4C trace messages. The traces are useful for watching how Log4C initializes itself, how it looks for and handles configuration files and environment variables, how it intializes and configures internal objects and for log time events like rollover events in the rollingfile appender, for example.

Also keep in mind that using the Log4C configuration file allows you complete control over which appender and layout your categories use. So if you are having problems with some specific appender or layout you can easily change those to compare the behavior with other appenders or layouts.

Deployment Experience with Log4C

This idea of this section is to gather any deployment experience or tips with deploying Log4C in projects.

Blame it on the logger

Here is some experience we had integrating Log4C into a new project. By it's nature a new project has bugs and also makes heavy use of logging to help with the development and debugging phase. What I observed was that on at least four occasions our system was crashing with a stack that led to some Log4C code. In none of these cases did the root cause turn out to be Log4C. After the fourth time debugging someone else's code I thought it worth while to look at the bugs to understand why there was a tendency to have the core stack terminate (misleadingly) in Log4C. Here are some observations that help explain that:

logging messages is relatively expensive--so once you turn on, especially heavy logging, then your code spends a lot of it time in there. It would be interesting to run a collector/analyzer to quantify that. So, in the case where there is general memory corruption in your system it is quite likely that when it does crash, it will be in your logging code. This accounts for 2 of the 4 bugs I mention that crashed Log4C

Often folks will put debug log statements (good!) without ever testing them (bad!). So some errors of the form of a mismatch between the format string and the parameter list can easily arise. These bugs at least have the merit of being easy to spot once they do occur. That was one of the 4 bugs.

Enabling logging calls obviously triggers an extra depth in the call stack--in the case of Log4C using the stream appender this can be up to 10 deep by the time you hit the base write() call to write to the file. So watch out if your stack size is small, especially if you are on a 64 bit system (where you double the memory consumed by pointer types) and especially if your code is already stack hungry. This of course describes exactly the scenario of the 4th bug that crashed Log4C. For information, on HP-UX, where we saw this problem, the default stack size for threads is 64K, which is pretty small compared with defaults of 1M on Solaris and Windows and 2M on Linux.

Another Log4C integrator shared this relevant experience:

on some Operating Systems there will be a maximum file size limit. If
log4c gets deployed in a production environment and no one watches and
rotates the log files, a crash and stackdump eventually results from the
standard C library routines that write to a file. This stack dump always
traces back through log4c and log4c can get blamed as the cause of the
crashes. Use rotating file appenders or tell your operations team to put
their own rotators on the files.

Preparing to move logging from development into a production environment.

At some point your project will actually be ready to ship and run. Here aresome considerations as you migrate logging from a development role to anoperations role. Operators will need to be able to debug problems without recompiling code.This is the most critical difference in how logging will be used once yourcode ships. Here are some tips.

Use the configuration file for configuration of categories and appenders, not the API.Operators will want do things like add additional appenders, redirectcategories to different appenders etc. They will not be able to just
recompile the code they way you do as a developer. If the configuration is
done by API then they cannot change anything. If the configuration is done
by the log4crc file then they can change things as needed.

Plan ahead for runtime debugging. Remember that once it ships, you
won't be able to just recompile to change or add logging. Make careful use
of the different log levels. Keep use of the ERROR level down to the
minimum, and use WARNING, INFO, and DEBUG as you get to messages that are
more verbose and less related to known bugs or problems. But don't hesitate
to put in lots of DEBUG statements. Your application will be run at ERROR
level in production until it has a problem. Picture yourself on the phone
telling someone how to change the level to DEBUG in the log4crc and they are
in a different city than you. If you have lots of extra DEBUG statements
available, it can mean the different between finding the problem just by
looking at logs, or having to reproduce the entire bug from inputs on a
system that simulates production. The first is quick, the second can
involve many days of setup before you can even properly simulate the
problem.

Use a logging hierarchy wisely to aid in pinpoint debugging.
Say that you have shipped several program that work together on and end
system. You can choose a logging hierarchy that makes it easy and flexible
to manage your logging. Say your company name is initech. And that you
have three applications that work together. And in each of these
applications you have input handling, business logic, and database access
logic. By using a hierarchy for your categories you can get great runtime
flexibility.
"company.program.subsystem"
Using this you would have logging to categories like
initech.prog1.dal
initech.prog2.blogic
initech.prog2.ilogic
This allows tremendous runtime flexibility so that operations can manage the
logging in many different ways without asking for recompiles and changes to
your code. In the above example the top level category "initech" would be
the main logging and would remain at ERROR level. If program 3 was having a
lot of difficulties and crashes, an additional logging would be added
mapping category initech.prog3 to a separate file at DEBUG level until the
problems could be solved. If program 2 was extremely slow and database
access was suspected, logging could be added to category initech.prog2.dal
at DEBUG level and put into its own file.

Referenceshttp://log4c.sf.net/ This SourceForge site is now the main log4c site.http://expat.sourceforge.net/http://gcc.gnu.orghttp://www.sunfreeware.com/http://hpux.connect.org.uk/Log4c developer mailing list Nice 'How to get developing with log4c' articleLog4jPantheios - "The C++ Logging Sweetspot"http://sourceforge.net/projects/log4cpp/