我可以找到log4j的解决方案,但不能找到log4cxx。
答案 0 :(得分:1)
我得到了一个简单的解决方案。
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
答案 1 :(得分:0)
我实现了针对log4j找到的c ++ this解决方案,创建了一个继承自Layout
的{{1}}类。它只是将标准PatternLayout
的输出包装在颜色转义序列中。
我将以下内容放在cpp文件中(如果您愿意,可以将其拆分为hpp / cpp):
log4cxx::PatternLayout
当然,配置你的记录器使用这个布局类,并像使用PatterLayout一样使用它,例如在#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");
}
文件中:
log4cxx.properties