I have created a log file using DCMTK.
Below is the sample code:
Code: Select all
#include <boost/dll.hpp>
#include <boost/filesystem.hpp>
#include <dcmtk/oflog/oflog.h>
#include <dcmtk/oflog/configrt.h>
#include "dcmtk/config/osconfig.h"
#include "dcmtk/oflog/oflog.h"
using namespace std;
using namespace dcmtk::log4cplus;
extern OFLogger sample_logger;
#define LINFO(msg) OFLOG_INFO(sample_logger, msg)
#define LDEBUG(msg) OFLOG_DEBUG(sample_logger, msg)
#define LERROR(msg) OFLOG_ERROR(sample_logger, msg)
#define LWARN(msg) OFLOG_WARN(sample_logger, msg)
#define LTRACE(msg) OFLOG_TRACE(sample_logger, msg)
#define LFATAL(msg) OFLOG_FATAL(sample_logger, msg)
class LogHandler
{
public:
std::string logLevel;
void InitializeLogger(std::string fileName);
void SetLogProperties(helpers::Properties& props, std::string fileName, std::string logLevel);
};
void SetLogProperties(helpers::Properties& props, std::string fileName, std::string logLevel)
{
#define SET(propertyKey, propertyValue) props.setProperty(propertyKey, propertyValue)
static const char* pattern = "[%-6i][%-6t][%D{%Y-%m-%d %H:%M:%S.%q}] %-5p: %m%n";
SET("log4cplus.logger.Service", "ALL");
SET("log4cplus.logger.Session", "ALL");
SET("log4cplus.appender.logfile", "log4cplus::RollingFileAppender");
if (logLevel == "debug")
{
SET("log4cplus.rootLogger", "DEBUG, console , logfile");
SET("log4cplus.appender.console", "log4cplus::ConsoleAppender");
SET("log4cplus.appender.console.ImmediateFlush", "true");
SET("log4cplus.appender.console.layout", "log4cplus::PatternLayout");
SET("log4cplus.appender.console.layout.ConversionPattern", pattern);
SET("log4cplus.appender.logfile.MaxFileSize", "5MB");
}
else
{
SET("log4cplus.rootLogger", "DEBUG, logfile");
SET("log4cplus.appender.logfile.MaxFileSize", "3MB");
}
SET("log4cplus.appender.logfile.Append", "true");
SET("log4cplus.appender.logfile.ImmediateFlush", "true");
SET("log4cplus.appender.logfile.MaxBackupIndex", "10");
SET("log4cplus.appender.logfile.File", fileName.c_str());
SET("log4cplus.appender.logfile.layout", "log4cplus::PatternLayout");
SET("log4cplus.appender.logfile.layout.ConversionPattern", pattern);
#undef SET
}
void InitializeLogger(std::string fileName)
{
helpers::Properties props(DCMTK_LOG4CPLUS_EXPORT(""));
if (props.size() == 0)
SetLogProperties(props, fileName, "debug");
unsigned int flags = 0;
flags |= PropertyConfigurator::fRecursiveExpansion;
flags |= PropertyConfigurator::fShadowEnvironment;
PropertyConfigurator conf(props, Logger::getDefaultHierarchy(), flags);
conf.configure();
Logger root = Logger::getRoot();
root.setLogLevel(OFLogger::INFO_LOG_LEVEL);
OFLog::reconfigure();
}
int main()
{
InitializeLogger("D:/log/sample.log");
// here I have read a json file and print the same
return 0;
}
Code: Select all
Sample.log.1
Sample.log.2
Sample.log.3
...
Sample.log.98
Sample.log.99
Sample.log.100
Sample.log.99 is renamed to Sample.log.100
Sample.log.98 is renamed to Sample.log.99
Sample.log is renamed to Sample.log.1
However, during this renaming process, some log files are getting missed or lost.
I have attached collapsed log files screen shot.
https://drive.google.com/file/d/1RKN32L ... drive_link
Thanks!