我需要将所有log4j日志文件保存在一个单独的月份文件夹中。因为实例认为这个1月份日志应该保存在1月文件夹中,一旦2月开始它应该在2月份文件夹内创建,反之亦然。 / p>
任何人都可以帮我实现这个目标吗?
先谢谢。
下面是mylog 4j config:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d
{yyyy-MM-dd HH:mm:ss} %-5p %c %3x - %m%n
Log to file FILE
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=D:\logfile.log
log4j.appender.file.DatePattern='.'dd/MM/yyyy
log4j.appender.file.append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= %m%n
下面是我的java代码:
final static Logger logger = Logger.getLogger(CarParser1.class);
public class CarParser1 {
final static Logger logger = Logger.getLogger(CarParser1.class);
static validatexml vxx=new validatexml();
static mailer mailobj=new mailer();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date=new Date();
public String getHeader()
{
logger.info("************************************
************************************
****************************************");
logger.info("
CAR VALIDATOR
V1.1 ");
logger.info("
By Software Team, Thomson Digital,
India.
");
logger.info("***************************
********************************
*****************************************************");
logger.info("\n\n");
logger.info("\nExecution Date & Time :"+dateFormat.format(date));
logger.info("********************************
*******************************************");
return "";
}
答案 0 :(得分:1)
如果你使用的是linux机器,你可以使用像这样的shell脚本
ListItem item_1 = new ListItem();
item_1.Name = "Name 1";
item_1.Email = "Email 1";
item_1.Phone = "Phone 1";
ListItem item_2 = new ListItem();
item_2.Name = "Name 2"; // This need to be changed from item_1 to item_2
item_2.Email = "Email 2"; // This need to be changed from item_1 to item_2
item_2.Phone = "Phone 2"; // This need to be changed from item_1 to item_2
ListItem item_3 = new ListItem();
item_3.Name = "Name 3"; // This need to be changed from item_1 to item_3
item_3.Email = "Email 3"; // This need to be changed from item_1 to item_3
item_3.Phone = "Phone 3"; // This need to be changed from item_1 to item_3
ListItem item_4 = new ListItem();
item_4.Name = "Name 4"; // This need to be changed from item_1 to item_4
item_4.Email = "Email 4"; // This need to be changed from item_1 to item_4
item_4.Phone = "Phone 4"; // This need to be changed from item_1 to item_4
并编写一个将运行一次的cron作业 在一个月内
MONTH=`date +%m`
YEAR=`date +%Y`
# Backup directory for logs
BKDIR="/logbackups/$YEAR/$MONTH"
#create directory
if [ ! -d "$BKDIR" ]; then
mkdir -p $BKDIR
fi
find /path/to/logs/ -type f -mtime +30 -exec mv {} /logbackups/$YEAR /$MONTH/ \;
答案 1 :(得分:0)
Log4j仅支持使用“org.apache.log4j.DailyRollingFileAppender”进行每日滚动日志记录。您可以使用受损方法(如shell脚本)或创建自定义的appender。