apache java log4j how to display a message console and file

I work with apache tomcat, java and log4j.

I have the following log4j.properties

log4j.rootLogger=INFO, CONSOLA
log4j.rootCategory=CONSOLA
log4j.logger.es.marm.regmaq=DEBUG,REGMAQ
log4j.logger.es.marm.comunes=DEBUG,REGMAQ
log4j.logger.es.magrama=DEBUG,REGMAQ
log4j.logger.es.ministerio=DEBUG,REGMAQ
log4j.logger.org.displaytag=INFO,DISPLAYTAG
log4j.logger.org.apache.axis=INFO,AXIS
log4j.logger.org.apache.commons=INFO,OTROS
log4j.logger.org.apache.xml=INFO,OTROS
log4j.logger.org.apache.ws=INFO,OTROS
log4j.logger.org.directwebremoting=INFO,OTROS
log4j.logger.jdbc.sqlonly=INFO, REGMAQ
log4j.logger.jdbc=INFO,REGMAQ
log4j.logger.jdbc.sqlonly=INFO,REGMAQ
log4j.logger.log4jdbc=INFO,REGMAQ

# Log para SQL: net.sf.log4jdbc.ConnectionSpy
log4j.logger.jdbc=FATAL,REGMAQ, CONSOLA,
log4j.logger.jdbc.sqlonly=INFO,REGMAQ, CONSOLA
log4j.logger.log4jdbc=FATAL,REGMAQ, CONSOLA
log4j.additivity.jdbc.sqlonly=false
#Querys en una sola linea
log4jdbc.dump.sql.maxlinelength=0
log4jdbc.trim.sql.extrablanklines=false

# Sistema de log de modo CONSOLA
log4j.appender.CONSOLA=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLA.Threshold=DEBUG, ERROR, FATAL, INFO
log4j.appender.CONSOLA.layout=es.ministerio.comunes.log4j.MinisterioLayout


# Sistema de log de modo FICHERO.
log4j.appender.REGMAQ=es.ministerio.comunes.log4j.RollingFileAppender
log4j.appender.REGMAQ.File=C:/tomcat/home/oramap04/regmaq/logs/regmaq.log
log4j.appender.REGMAQ.Append=true
log4j.appender.REGMAQ.ImmediateFlush=true
log4j.appender.REGMAQ.Threshold=DEBUG
log4j.appender.REGMAQ.encoding=ISO-8859-1
log4j.appender.REGMAQ.MaxFileSize=5MB
log4j.appender.REGMAQ.MaxBackupIndex=30

# Sistema de log de modo AXIS.
log4j.appender.AXIS=es.ministerio.comunes.log4j.RollingFileAppender
log4j.appender.AXIS.File=C:/tomcat/home/oramap04/regmaq/logs/axis.log
log4j.appender.AXIS.Append=true
log4j.appender.AXIS.ImmediateFlush=true
log4j.appender.AXIS.Threshold=INFO
log4j.appender.AXIS.MaxFileSize=5MB
log4j.appender.AXIS.MaxBackupIndex=10

# Sistema de log de modo DISPLAYTAG.
log4j.appender.DISPLAYTAG=es.ministerio.comunes.log4j.RollingFileAppender
log4j.appender.DISPLAYTAG.File=C:/tomcat/home/oramap04/regmaq/logs/displaytag.log
log4j.appender.DISPLAYTAG.Append=true
log4j.appender.DISPLAYTAG.ImmediateFlush=true
log4j.appender.DISPLAYTAG.Threshold=INFO
log4j.appender.DISPLAYTAG.MaxFileSize=5MB
log4j.appender.DISPLAYTAG.MaxBackupIndex=2

# Sistema de log de modo OTROS.
log4j.appender.OTROS=es.ministerio.comunes.log4j.RollingFileAppender
log4j.appender.OTROS.File=C:/tomcat/home/oramap04/regmaq/logs/otros.log
log4j.appender.OTROS.Append=true
log4j.appender.OTROS.ImmediateFlush=true
log4j.appender.OTROS.Threshold=INFO
log4j.appender.OTROS.MaxFileSize=5MB
log4j.appender.OTROS.MaxBackupIndex=10

In a java class, I have two log.info.

I work with sql sentences. I want to write the sentences sql in a file and in the console. The two log.info and the sentences sql are written in the file.

How can I write the log.info and the sentences sql in console also?

1 answer

  • answered 2019-11-08 14:09 AswathyPrasad

    Have you tried changing Layout to a simple one ?

    #define the layout
    log4j.appender.CONSOLA.layout = org.apache.log4j.PatternLayout
    log4j.appender.CONSOLA.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] [%c] [%M] [%l] - %m%n
    

    If that works, probably something with your conversion pattern - es.ministerio.comunes.log4j.MinisterioLayout