How to set colour of console output of log4cxx?

喜欢而已 提交于 2019-12-10 19:22:10

问题


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


来源:https://stackoverflow.com/questions/14118703/how-to-set-colour-of-console-output-of-log4cxx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!