How to set colour of console output of log4cxx?

2019-07-07 08:42发布

I can find solution for log4j, but not for log4cxx.

标签: c++ log4cxx
2条回答
仙女界的扛把子
2楼-- · 2019-07-07 09:10

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
查看更多
三岁会撩人
3楼-- · 2019-07-07 09:31

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
查看更多
登录 后发表回答