首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求会用log4cxx好手指导:未能够产生每天log

2013-01-17 
求会用log4cxx高手指导:未能够产生每天log本帖最后由 death10001 于 2013-01-06 15:39:01 编辑log4j.rootL

求会用log4cxx高手指导:未能够产生每天log
本帖最后由 death10001 于 2013-01-06 15:39:01 编辑

log4j.rootLogger=DEBUG,DAILY_FILE
log4j.appender.DAILY_FILE=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.DAILY_FILE.file=D:/logs/logging.log4j
log4j.appender.DAILY_FILE.DatePattern='_'yyyy-MM-dd'.log' 
log4j.appender.DAILY_FILE.ImmediateFlush=true
log4j.appender.DAILY_FILE.Append=true
log4j.appender.DAILY_FILE.layout=org.apache.log4j.PatternLayout 
log4j.appender.DAILY_FILE.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n 



上网找了及试了他们说行的代码,都不能产生每天一个日志log的。我的都是生成log4j 不管怎么修改电脑日期还是没有产生log4j_2013-01-06.log。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
int _tmain(int argc, _TCHAR* argv[])
{
/*log4cxx::PropertyConfigurator::configure("log4cxx.properties");*/

LOG4CXX_INFO(log4cxx::Logger::getLogger("hello"), "你好,log4cxx!");
LOG4CXX_DEBUG(log4cxx::Logger::getLogger("hello"), "hello,DEBUG!");
LOG4CXX_ERROR(log4cxx::Logger::getLogger("hello"), "hello,ERROR!");
LOG4CXX_WARN(log4cxx::Logger::getLogger("hello"), "hello,WARN!");

char errorbuf[80] = {0};
sprintf(errorbuf, "TimeRsp.nErrno: %d", 123);
LOG4CXX_INFO(log4cxx::Logger::getLogger("hello"), errorbuf);


return 0;
}

求高手指导?我想每小时或者每天或者每月或者每年产生一个带日期的log.
在网上找了好久 没找到相关的问题解答,试了很多他们的代码也不能产生。只产生一个logging.log4j文件。 log4j C++
[解决办法]
仅供参考
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt
------解决方案--------------------


0==pszFmt[0]) return;
    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}


[解决办法]
http://blog.chinaunix.net/uid-25808509-id-3034346.html
[解决办法]
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。

热点排行