Hot questions for Using GlassFish in log4j

Question:

sql_show = true

this property in hibernate prints the sql that is run, but i want to see the begin transaction and complete transaction statements as well so that i can track the transaction duration and see the query run in which transaction.

googling reveals that

log4j.logger.org.hibernate.SQL = DEBUG, defaultAppender
log4j.logger.org.hibernate.type = DEBUG, defaultAppender
log4j.logger.org.hibernate.transaction=DEBUG, defaultAppender

should show you the transaction level data as well. But it doesnt.

Investigating more i looked into hibernate code and found a class name

org.hibernate.ejb.TransactionImpl

this class has the begin and complete method but this method does not log any thing.

Any advice how to see the transaction level info in hibernate ? I am using hibernate 2.2


Answer:

For Hibernate 5
  • For SLF4J logging:

    <logger name="org.hibernate.engine.transaction.internal.TransactionImpl" level="debug"/>
    
  • For Log4j:

     <logger name="org.hibernate.engine.transaction.internal.TransactionImpl">
          <level value="DEBUG"/>
     </logger>
    
For Hibernate 4

You need to set the logging threshold to DEBUG for the following classes:

  1. For JDBC transactions (e.g. RESOURCE_LOCAL)

    • For SLF4J logging:

      <logger name="org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction" level="debug"/>
      
    • For Log4j:

      <logger name="org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction">
         <level value="DEBUG"/>
      </logger>
      
  2. For JTA transactions

    • For SLF4J logging:

      <logger name="org.hibernate.engine.transaction.internal.jta.JtaTransaction" level="debug"/>
      
    • For Log4j:

      <logger name="org.hibernate.engine.transaction.internal.jta.JtaTransaction">
         <level value="DEBUG"/>
      </logger>
      

It's better to activate the DEBUG level for as few classes as possible because otherwise, your logs size will increase dramatically.

Question:

I have many applications (war, ejb) in same glassfish server, the applications have different log4j configuration

app1.war -> log4j.properties

log4j.rootLogger=info, infcore
log4j.appender.infcore=org.apache.log4j.RollingFileAppender
log4j.appender.infcore.File=/var/log/app1info.log
log4j.appender.infcore.MaxFileSize=20000KB
log4j.appender.infcore.MaxBackupIndex=20
log4j.appender.infcore.layout=org.apache.log4j.PatternLayout
log4j.appender.infcore.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n

app2.jar -> log4j.properties

log4j.rootLogger=info, infoejb
log4j.appender.infoejb=org.apache.log4j.RollingFileAppender
log4j.appender.infoejb.File=/var/log/ejbinfo.log
log4j.appender.infoejb.MaxFileSize=20000KB
log4j.appender.infoejb.MaxBackupIndex=20
log4j.appender.infoejb.layout=org.apache.log4j.PatternLayout
log4j.appender.infoejb.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n

app3.war -> log4j.properties

log4j.rootLogger=info, infoapp2
log4j.appender.infoapp2=org.apache.log4j.RollingFileAppender
log4j.appender.infoapp2.File=/var/log/infoapp2.log
log4j.appender.infoapp2.MaxFileSize=20000KB
log4j.appender.infoapp2.MaxBackupIndex=20
log4j.appender.infoapp2.layout=org.apache.log4j.PatternLayout
log4j.appender.infoapp2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%5p] [%t] %c %x - %m%n

I call logger:

protected static final Logger LOG = Logger.getLogger(JAXSecurutyValidation.class);

The problem is that the information is recorded in only one file the ejb, the others files are not created.

How can I fix it?

SOLVED I find the solution, is very simple update to glasfish 5 and all works fine, thanks.


Answer:

Maybe this can help you:

String log4jfile = "log4 file path";
PropertyConfigurator.configure(log4jfile);

You can call each log4j file for each application.

Question:

I am using Payara 4.1 and Netbeans 8.1.

When I run my application, these four lines are among the first to be logged:

 #!## LogManagerService.postConstruct : rootFolder=/opt/payara41/glassfish
 #!## LogManagerService.postConstruct : templateDir=/opt/payara41/glassfish/lib/templates
 #!## LogManagerService.postConstruct : src=/opt/payara41/glassfish/lib/templates/logging.properties
 #!## LogManagerService.postConstruct : dest=/opt/payara41/glassfish/domains/domain1/config/logging.properties

I added the line org.springframework=WARNING at the end of the last logging.properties file given above, and restarted my server. That didn't seem to have an effect. When I open the asadmin shell /opt/payara41/bin/asadmin and run list-log-attributes, here is what I get:

asadmin> list-log-attributes
com.sun.enterprise.server.logging.GFFileHandler.excludeFields   <>
com.sun.enterprise.server.logging.GFFileHandler.file    <${com.sun.aas.instanceRoot}/logs/server.log>
com.sun.enterprise.server.logging.GFFileHandler.flushFrequency  <1>
com.sun.enterprise.server.logging.GFFileHandler.formatter   <com.sun.enterprise.server.logging.ODLLogFormatter>
com.sun.enterprise.server.logging.GFFileHandler.logtoConsole    <false>
com.sun.enterprise.server.logging.GFFileHandler.maxHistoryFiles <0>
com.sun.enterprise.server.logging.GFFileHandler.multiLineMode   <true>
com.sun.enterprise.server.logging.GFFileHandler.retainErrorsStasticsForHours    <0>
com.sun.enterprise.server.logging.GFFileHandler.rotationLimitInBytes    <2000000>
com.sun.enterprise.server.logging.GFFileHandler.rotationOnDateChange    <false>
com.sun.enterprise.server.logging.GFFileHandler.rotationTimelimitInMinutes  <0>
com.sun.enterprise.server.logging.SyslogHandler.useSystemLogging    <false>
handlerServices <com.sun.enterprise.server.logging.GFFileHandler,com.sun.enterprise.server.logging.SyslogHandler>
handlers    <java.util.logging.ConsoleHandler>
java.util.logging.ConsoleHandler.formatter  <com.sun.enterprise.server.logging.UniformLogFormatter>
java.util.logging.FileHandler.count <1>
java.util.logging.FileHandler.formatter <java.util.logging.XMLFormatter>
java.util.logging.FileHandler.limit <50000>
java.util.logging.FileHandler.pattern   <%h/java%u.log>
log4j.logger.org.hibernate.validator.util.Version   <warn>
org.springframework <WARNING>
Command list-log-attributes executed successfully.

I have tried the suggestions given in this SO question but that didn't work. I'm stumped. :( I really do not want to see all those INFO logging lines printed by the spring framework.

=== EDIT ===

Here is what is in my log4j.properties (copied from SO):

# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout

# Define the file appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# Set the name of the logs destination
log4j.appender.stdout.target=System.out

# Set the immediate flush to true (default)
log4j.appender.stdout.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.stdout.Threshold=debug

# Set the append to false, overwrite
log4j.appender.stdout.Append=false

# Define the layout for appender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=%d{yyyy-MM-dd}:%m%n

log4j.logger.org.springframework=WARNING

Here are all the places I have copied that file to:

$PROJECT_DIR/src/log4j.properties
$PROJECT_DIR/src/main/resources/META-INF/log4j.properties
$PROJECT_DIR/src/main/webapp/WEB-INF/log4j.properties
$PROJECT_DIR/src/main/webapp/WEB-INF/classes/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/classes/log4j.properties
$PROJECT_DIR/target/$PROJECT-1.0/WEB-INF/classes/META-INF/log4j.properties
$PROJECT_DIR/target/classes/META-INF/log4j.properties
/opt/payara41/glassfish/domains/domain1/config/log4j.properties

In addition, I have added log4j.logger.org.springframework=WARNING and org.springframework=WARNING to Configurations > server-config > Logger Settings > Module Log Levels in the Payara UI. Nothing is working. Payara's server log still has INFO lines for the spring framework.


Answer:

I tried everything that has been suggested so far, to no avail. So I ended up doing something pretty hacky. I wrote my own log handler and installed it to domain1/lib/ext:

package org.springsuppressor;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;

public class MyHandler extends ConsoleHandler {

  public MyHandler() {
    super();
    setLevel(Level.WARNING);
  }
}

The handler above sets the log level for all loggers to WARNING. I could suppress messages for just the spring framework, but I don't yet need to do that.

Question:

I have a web app that uses log4j. I have configured a custom logger in the /WEB-INF/classes/log4j.properties file like the following :

log4j.rootLogger=ERROR, CNSL

log4j.appender.CNSL=org.apache.log4j.ConsoleAppender
log4j.appender.CNSL.Target=System.out
log4j.appender.CNSL.layout=org.apache.log4j.PatternLayout
log4j.appender.CNSL.layout.ConversionPattern=%d{HH:mm:ss:SSS} %p %c (%F:%L) - %m%n


log4j.logger.my.app = INFO

The log messages are written to the server.log as expected but am trying to find a way to control the custom logger log levels via the glassfish server.

I tried creating the logger in glassfish from the command :

asadmin set-log-level my.app.level=WARNING 

however all INFO messages are still logged even though the logger is set to WARNING by the asadmin command. Is there a way to control the logger level of the custom loggers configured in the log4j.properties from the glassfish loggers?


Answer:

You can control the log level through the console in Glassfish if you use java.util.logging as underlying logging framework in your web application.

You can also use SLF4J in your code and use java.util.logging as underlying logging framework (through slf4j-jdk14.jar).