Earlier
we reviewed the logging library Pantheios. The turn has come to familiarize with more known Apache log4cxx. The library is the implementation of the principles and mechanisms of log4j (Java logging library) in C ++. The project looks “dead”, at least there have been no drastic changes over the past year, however this is not a reason not to look (and maybe use) this library.
General remarks
I am reviewing the process of building and working with the library on a Windows platform using VisualStudio 2010 SP1. Logs as usual want to have both on the console and in the file. It should be emphasized that the logs may contain Simols of national alphabets (read unicode).
The article is designed for beginners and contains material for a quick start, and not a deep study of the library.
')
Getting the library
Assembly
First we need to download the dependencies and the library itself. For good, you should download it from the
SVN project , since judging by the commits, the bugs were fixed, but for some reason I decided not to risk and try my luck with the stable version 0.10.0.
So let's go!
Downloading:
: www.apache.org/dyn/closer.cgi/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz
APR: apr.apache.org/download.cgi ( APR 1.4.2, APR-util 1.3.10 , APR iconv 1.2.1)
Sed: sourceforge.net/projects/gnuwin32/files//sed/4.2.1/sed-4.2.1-bin.zip/download
Sed:
sourceforge.net/projects/gnuwin32/files/libintl/0.14.4/libintl-0.14.4-bin.zip/download
sourceforge.net/projects/gnuwin32/files/libiconv/1.9.2-1/libiconv-1.9.2-1-bin.zip/download
sourceforge.net/projects/gnuwin32/files/regex/2.7/regex-2.7-bin.zip/download
Fuf, the hardest thing behind ...
Unpack:
- sources in C: \ temp \ log4cxx_test \ apache-log4cxx-0.10.0 \
- APR in C: \ temp \ log4cxx_test \ apr \ (do not forget to rename the archive folder)
- APR Util in C: \ temp \ log4cxx_test \ apr-util \ (do not forget to rename the archive folder)
- APR in C: \ temp \ log4cxx_test \ apr-util \ (do not forget to rename the archive folder)
I do not need sed on the machine as such, but it is needed for the configuration script, I should throw it here: C: \ temp \ log4cxx \ apache-log4cxx-0.10.0 \ and all the dll files on which it depends next to put. Estets can throw both sed and dll in% SystemRoot% \ System32 \ if they need sed in the future.
Next: run the VS2010 command line and go to the C: \ temp \ log4cxx \ apache-log4cxx-0.10.0 \ directory and do it sequentially:
>configure
>configure-aprutil
Errors should not be.
It is the turn of the actual assembly. Here you should launch the studio itself (devenv) and open c: \ temp \ log4cxx \ apache-log4cxx-0.10.0 \ projects \ log4cxx.dsw in it. The studio will ask to convert projects that we need.
Click build, but do not drink tea. The assembly process will be completed fairly quickly ~ 300 mi errors. Will swear on the LOG4CXX_LIST_DEF macro. All such macros must be rendered before the class (they are declared inside the class).
After removing these errors, there will be errors associated with the unknown std :: insert_iterator - just add #include <iterator> at the beginning of the file.
Further, there will be errors about the “KeySet” uncertainty - all such errors should be changed (example):
LoggingEvent::KeySet set;
KeySet set;
(since we have taken out the macro that forms these same KeySets outside the classes).
After this “shamanism” the build will be successful, but the linkage will “fill up” with messages about the uncertainty of some functions. This is just a studio when converting forgot to make links to the necessary files in the project.
They simply need to be added to the properties of the project log4cxx (I zahardkodil paths for simplicity absolute, because I need it only for the period of a one-time assembly):


after which the link will work.
Briefly about architecture
log4cxx uses the concepts of appenders, layouts and target s.
appender - defines a set of parameters for the recipient (target) of the message flow (a log of the lines in essence), contains a link to the layout, which in turn defines the rules for formatting the message.
More information about the architecture can be found
on the project website .
Testing work
I prefer to tighten similar fundamental libraries into my programs through variable environments (but since I work with different versions of libraries I try not to define them as environment variables for the user). Therefore, running the command line VS2010 we drive about the following:
>SET LOG4CXX=C:\temp\log4cxx\apache-log4cxx-0.10.0\src\main\include\
>SET LOG4CXXLIB=C:\temp\log4cxx\apache-log4cxx-0.10.0\projects\Debug\
>devnenv
(I collected in debug mode, for release, of course, the paths are slightly different).
Next, create a simple project C ++ Console Application - this will be our test project.
In it, we will define paths to common include files and libraries in the project settings, according to our environment variables:

and for linking:


It was the turn to add the necessary include us:
Now it is necessary to make a configuration file. I made it like this:
<?xml version="1.0" encoding="UTF-8" ?> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" > <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %level %c{2}#%M %F:%L - %m%n"/> </layout> </appender> <appender name="DailyRollingFileAppender" class="org.apache.log4j.rolling.DailyRollingFileAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %level %c{2}#%M %F:%L - %m%n"/> </layout> <param name="Encoding" value="UTF-16" /> <param name="file" value="logfile.log"/> <param name="DatePattern" value="'.'yyyy-MM-dd" /> <param name="append" value="true"/> </appender> <root> <priority value="all" /> <appender-ref ref="DailyRollingFileAppender"/> <appender-ref ref="ConsoleAppender"/> </root> </log4j:configuration>
You can read more in the documentation for log4j, because file formats are "almost the same."
Well, below is the test program:
void foo(void*) { log4cxx::LoggerPtr logger = log4cxx::Logger::getRootLogger(); for(int i=0;i<10;++i){ LOG4CXX_DEBUG((logger), L"debug" << L"other debug message"); LOG4CXX_TRACE((logger), L"trace"); LOG4CXX_INFO((logger), L", !"); LOG4CXX_WARN((logger), L"WARN"); LOG4CXX_ERROR((logger), L"error"); LOG4CXX_FATAL((logger), L"FATAL"); } } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "Russian_Russia.OCP");
I note the "trick" of use:
LOG4CXX_DEBUG((logger), L"debug" << L"other debug message");
from the “how it works” cycle. The fact is that the second macro argument is passed to the input to stream, which is why you can write several messages here (not just text) using operator <<.
Conclusion
We looked at the build and basic setup of log4cxx using a simple example.
Of the advantages of the library, it is worth highlighting customizability through the configuration file, unicode support “out of the box” (including various encodings of the output files).
Of the minuses, it should be noted the relative complexity of the assembly, they also say that the library is flowing and not so fast.
Outside this topic remains: advanced configuration, comparison with other loggers, verification of the “myth of techs”.
Enjoy your logging!