I can find solution for log4j, but not for log4cxx.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I got a simple solution.
extern LoggerPtr logger;
#define LOG_TRACE(msg) LOG4CXX_TRACE(logger, msg)
#define LOG_DEBUG(msg) LOG4CXX_DEBUG(logger, msg)
#define LOG_INFO(msg) LOG4CXX_INFO(logger, msg)
#define LOG_INFO_H(msg) LOG4CXX_INFO(logger, "\33[0;42m" << msg << "\33[0m") // green
#define LOG_WARN(msg) LOG4CXX_WARN(logger, "\33[0;43m" << msg << "\33[0m") // yellow
#define LOG_ERROR(msg) LOG4CXX_ERROR(logger, "\33[0;41m" << msg << "\33[0m") // red
#define LOG_FATAL(msg) LOG4CXX_FATAL(logger, "\33[0;41m" << msg << "\33[0m") // red
回答2:
as google brought me here, I post my solution, hopping it can help someone.
I implemented for c++ this solution I found for log4j, creating a Layout
class inheriting from PatternLayout
. It simply wraps the output of the standard log4cxx::PatternLayout
in color escape sequences.
I put the following in a cpp file (split it in a hpp/cpp if you like):
#include <log4cxx/helpers/pool.h>
#include <log4cxx/patternlayout.h>
namespace log4cxx{
class LOG4CXX_EXPORT ColorPatternLayout : public log4cxx::PatternLayout
{
public:
DECLARE_LOG4CXX_OBJECT(ColorPatternLayout)
BEGIN_LOG4CXX_CAST_MAP()
LOG4CXX_CAST_ENTRY(ColorPatternLayout)
LOG4CXX_CAST_ENTRY_CHAIN(Layout)
END_LOG4CXX_CAST_MAP()
ColorPatternLayout();
ColorPatternLayout(const log4cxx::LogString &s);
virtual void format(log4cxx::LogString &output, const log4cxx::spi::LoggingEventPtr &event, log4cxx::helpers::Pool &pool) const override;
};
LOG4CXX_PTR_DEF(ColorPatternLayout);
}
using namespace log4cxx;
IMPLEMENT_LOG4CXX_OBJECT(ColorPatternLayout);
ColorPatternLayout::ColorPatternLayout() : log4cxx::PatternLayout(){}
ColorPatternLayout::ColorPatternLayout(const LogString &s): log4cxx::PatternLayout(s){}
void ColorPatternLayout::format(LogString &output, const spi::LoggingEventPtr &event, helpers::Pool &pool) const
{
log4cxx::LogString tmp;
log4cxx::PatternLayout::format(tmp,event,pool);
log4cxx::LevelPtr lvl = event->getLevel();
switch (lvl->toInt()){
case log4cxx::Level::FATAL_INT:
output.append("\u001b[0;41m"); //red BG
break;
case log4cxx::Level::ERROR_INT:
output.append("\u001b[0;31m"); // red FG
break;
case log4cxx::Level::WARN_INT:
output.append("\u001b[0;33m"); //Yellow FG
break;
case log4cxx::Level::INFO_INT:
output.append("\u001b[1m"); // Bright
break;
case log4cxx::Level::DEBUG_INT:
output.append("\u001b[2;32m"); // Green FG
break;
case log4cxx::Level::TRACE_INT:
output.append("\u001b[0;30m"); // Black FG
break;
default:
break;
}
output.append(tmp);
output.append("\u001b[m");
}
And of course configure your logger to use this layout class, and use it exactly like a PatterLayout, e.g in a log4cxx.properties
file:
log4j.appender.EXAMPLE=org.apache.log4j.ConsoleAppender
log4j.appender.EXAMPLE.layout=org.apache.log4j.ColorPatternLayout
log4j.appender.EXAMPLE.layout.ConversionPattern=%-5p %c %x - %m%n