Solved

Log abstraction layer and log4cpp

Posted on 2010-09-01
11
1,235 Views
Last Modified: 2013-11-12
I want to use log4cpp library with an abstraction layer like below
 
logLog4Cpp includes Appender, Category, Layout classes.

log4cpp
I don’t want to loose this capabilities becouse of abstraction layer.
If I add parameters which are needed by Log4Cpp library like below:

virtual void getLogger(const String& AppenderType, const String& CategoryName, const String& LayoutName, Boolean Additivity);

I have to change LoggerFactory interface’s getLogger method too. Soi that my interface become useless for other logging libraries.
How can I manage to use Log4Cpp’s capabilites? Could you suggest a convenient way?  
0
Comment
Question by:vileda
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
I recently implemented an abstraction layer for log4cxx so it became part of our standard toolkit but did not include a code level dependency on a specifically logger.

I can't share code because it is copyrighted but I don't mind assisting you with some pointers if you think this might be a solution for you.
0
 

Author Comment

by:vileda
Comment Utility
I've just looked log4xxx library. It looks similiar with log4cpp. If you could give me main ideas and the right path, it would help me alot. Thanks.
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 100 total points
Comment Utility
How about this, use a DTO --
class LoggerTraits {
};
class LoggerFactory {
 void getLogger(const String& AppenderType, LoggerTraits* pTraits);
};
------------------------------

class Log4CPP_LoggerTraits : public LoggerTraits {
public:
String CategoryName;
String LayoutName;
Boolean Additivity;
}
class Log4CPP_LoggerFactory : public LoggerFactory {
void getLogger(const String& AppenderType, LoggerTraits* pTraits) {
Log4CPP_LoggerTraits* myTraits = (Log4CPP_LoggerTraits*) pTraits;
...
}
};
0
 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
Comment Utility
>> I've just looked log4xxx library. It looks similiar with log4cpp
It is similar, but far more mature and better supported than log4cpp since it is part of the apache movement.

I implemented 3 specific set of macros, one for generic logging, one for function level logging and the other for class level logging. As you know, log4 allows you to define specific loggers at runtime, which can then be controlled by a runtime config script to enable or disable loggers and implement output formatting on them.

Below is an example of the kind of thing I did. There is a hell of a lot more to my framework than that, this is a very (VERY) simplified view of how you can implement macro layer abstraction. I'm not saying this is the best solution but it was nice and simple for my needs and may be suitable for yours. If your requirements are more complex; however. it probably won't work for you.

Either way, I would strongly recommend log4cxx over log4cpp :)



// For generic logging I implemented a set of macros that did something like this...



#define MLOG_TRACE(msg) LOG4CXX_TRACE(log4cxx::Logger::getLogger("default"), msg)

#define MLOG_DEBUG(msg) LOG4CXX_DEBUG(log4cxx::Logger::getLogger("default"), msg)

#define MLOG_INFO(msg) LOG4CXX_INFO(log4cxx::Logger::getLogger("default"), msg)

#define MLOG_WARN(msg) LOG4CXX_WARN(log4cxx::Logger::getLogger("default"), msg)

#define MLOG_ERROR(msg) LOG4CXX_ERROR(log4cxx::Logger::getLogger("default"), msg)

#define MLOG_FATAL(msg) LOG4CXX_FATAL(log4cxx::Logger::getLogger("default"), msg)



// For function level logging I did something like this...



#define MLOG_FUNC_TRACE(msg) LOG4CXX_TRACE(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)

#define MLOG_FUNC_DEBUG(msg) LOG4CXX_DEBUG(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)

#define MLOG_FUNC_INFO(msg) LOG4CXX_INFO(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)

#define MLOG_FUNC_WARN(msg) LOG4CXX_WARN(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)

#define MLOG_FUNC_ERROR(msg) LOG4CXX_ERROR(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)

#define MLOG_FUNC_FATAL(msg) LOG4CXX_FATAL(log4cxx::Logger::getLogger(BOOST_CURRENT_FUNCTION), msg)



// For class level logging I did something like this...



#define MLOG_CLASS_TRACE(msg) LOG4CXX_TRACE(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

#define MLOG_CLASS_DEBUG(msg) LOG4CXX_DEBUG(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

#define MLOG_CLASS_INFO(msg) LOG4CXX_INFO(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

#define MLOG_CLASS_WARN(msg) LOG4CXX_WARN(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

#define MLOG_CLASS_ERROR(msg) LOG4CXX_ERROR(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

#define MLOG_CLASS_FATAL(msg) LOG4CXX_FATAL(log4cxx::Logger::getLogger(typeid(*this).name()), msg)

Open in new window

0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
@evilrix: Thanks for the link, I was looking for something like that but it always eluded me - always found log4cpp and log4cplus.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
If you are using a well-supported, cross-platform logging library like log4cxx, I'd say there's no reason to have an abstraction layer around it ... It would only serve to complicate and slow down the code - probably introducing some bugs at the same time.

What is the reason for wanting an abstraction layer ?
0
 

Author Comment

by:vileda
Comment Utility

It's adviced by some writers (like below link) to reduce dependency and to achive a loosely coupled design.

http://www.amazon.com/Code-Leader-Processes-Successful-Programmer/dp/0470259248
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
Did you have a look at my earlier comment?
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
Comment Utility
>> It's adviced by some writers (like below link) to reduce dependency and to achive a loosely coupled design.

My point is that using the log4cxx (or similar) library, already achieves "loose coupling", since it already abstracts logging (it's cross-platform, and provides a unified, commonly used interface).

There's a point where adding abstractions just for the sake of adding them, does more harm than good to a project. And unless there is a really good reason, I would not recommend complicating the design by adding more abstraction layers.
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> My point is that using the log4cxx (or similar) library, already achieves "loose coupling"

You'll note that all my abstraction layer does is removes the explicit need to define a logger (something specific to the log4 logger) so that if you do choose to use a different library in the future your code does not been to be molested to incorporate it.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> so that if you do choose to use a different library in the future your code does not been to be molested to incorporate it.

If it's estimated that there is a very high probability that such a change will happen in the future, and if it's estimated that it's more cost-effective to put effort into making the abstraction now, rather than modifying the code when it's needed, then adding an abstraction layer might be a good idea.

I'm just talking from the general point of view, and from experience : most of the constructs put into place in code to facilitate possible future changes to the code, will never be used. In other words : most such changes are wasted effort, and result in needless complication (with performance loss and additional bugs as likely symptoms).

Note that I said "most", not "all" :)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Before You Read The Article Please make sure you understand these two concepts: Variable Scope (http://www.php.net/manual/en/language.variables.scope.php) and Property Visibility (http://www.php.net/manual/en/language.oop5.visibility.php).  And to …
The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now