Reputation: 303
I need to keep all my log4j log files in a separate month wise folder.for Instance consider this January month logs should be kept inside January folder,once February starts it should be created inside that February month folder and vice versa.
Can anyone help me to achieve this.?
Thanks in Advance.
Below is 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
below are my java codes:
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 "";
}
Upvotes: 1
Views: 1056
Reputation: 228
Log4j only supports daily rolling logging with "org.apache.log4j.DailyRollingFileAppender". You can use compromised methods such as the shell script or create a customized appender.
Upvotes: 1
Reputation: 76
If you are using a linux machine you could use a shell script like this
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/ \;
and write a cron job that will run once in a month
0 0 1 * * /path/to/script
Upvotes: 1