1

I try to log in categories, but I have no idea how to configure my log4j.properties.

What do I want exactly:

  • Daily Logfile for Categories {SECURITY, DATABASE, MAIL}
  • Logfile has name of its category
  • Log entries with the class that called the logger (ex. below)

Here is what I have:

public class CategoryLogger {

    private static Logger securityLogger = Logger.getLogger("SECURITY");
    private static Logger databaseLogger = Logger.getLogger("DATABASE");
    private static Logger mailLogger = Logger.getLogger("MAIL");


    public void securitylog(LogLevel level, String message) {
        this.log(securityLogger, level, message);
    }

    public void databaselog(LogLevel level, String message) {
        this.log(databaseLogger, level, message);
    }

    public void maillog(LogLevel level, String message) {
        this.log(mailLogger, level, message);
    }

    private void log(Logger logger, LogLevel level, String message) {
        switch (level) {
            case TRACE:
                logger.trace(message);
                break;
            case DEBUG:
                logger.debug(message);
                break;
            case INFO:
                logger.info(message);
                break;
            case WARN:
                logger.warn(message);
                break;
            case ERROR:
                logger.error(message);
                break;
            case FATAL:
                logger.fatal(message);
                break;
            default:
                break;
        }
    }
}

The logs should look like this:

database-2018-09-28.log
DATABASE | 2018-09-28 20:02:19 | INFO  | DAO:22 | This is a info.
DATABASE | 2018-09-28 20:02:19 | WARN  | DAO:23 | This is a warn.
DATABASE | 2018-09-28 20:02:19 | ERROR | DAO:24 | This is a error.
DATABASE | 2018-09-28 20:02:19 | FATAL | DAO:25 | This is a fatal.

security-2018-09-28.log
SECURITY | 2018-09-28 20:02:19 | INFO  | SimpleAccess:33 | This is a info.
SECURITY | 2018-09-28 20:02:19 | WARN  | SimpleAccess:34 | This is a warn.
SECURITY | 2018-09-28 20:02:19 | ERROR | SimpleAccess:35 | This is a error.
SECURITY | 2018-09-28 20:02:19 | FATAL | SimpleAccess:36 | This is a fatal.

mail-2018-09-28.log
MAIL | 2018-09-28 20:02:19 | INFO  | MailSender:44 | This is a info.
MAIL | 2018-09-28 20:02:19 | WARN  | MailSender:45 | This is a warn.
MAIL | 2018-09-28 20:02:19 | ERROR | MailSender:46 | This is a error.
MAIL | 2018-09-28 20:02:19 | FATAL | MailSender:47 | This is a fatal.

Thanks a lot in advance for any help!

Kind Regards,

L.

silenum
  • 149
  • 1
  • 2
  • 11
  • 1
    [This will help with the filename](https://stackoverflow.com/questions/1324053/configure-log4j-to-log-to-custom-file-at-runtime) – Nicholas K Sep 28 '18 at 18:57

1 Answers1

2

A possible solution is to use 3 separate file appender (one for each logger)

for example for the database logs add the followings to your log4j.properties

log4j.appender.DATABASE_RFA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DATABASE_RFA.File=/path/to/your/log/folder/database-
log4j.appender.DATABASE_RFA.DatePattern='.'yyyy-MM-dd'
log4j.appender.DATABASE_RFA.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE_RFA.layout.ConversionPattern=DATABASE | %d{yyyy-MM-dd HH:mm:ss} | %p | %c{1}:%L | %m%n



log4j.logger.DATABASE = info, DATABASE_RFA 
log4j.logger.SECURITY = info, SECURITY_RFA 
log4j.logger.MAIL = info, MAIL_RFA 

you have to repeat the appender section for SECURITY and MAIL loggers

gtosto
  • 1,381
  • 1
  • 14
  • 18
  • how does the databaselogger know then, that he should writer to the DATABASE_RFA? – silenum Oct 02 '18 at 13:58
  • It is stated by the row log4j.logger.DATABASE = info, DATABASE_RFA Which will send log events to the appender DATABASE_RFA that actually write into the database file – gtosto Oct 02 '18 at 14:55