简体   繁体   中英

How can I display the CLASSNAME in the log for PLAY Java?

Why doesn't Play show/output the classname in the log?

Here's my logger config:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/application.log</file>
    <encoder>
        <pattern>%date - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>logs/debug_log.%d{yyyy-MM-dd}.log
        </fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%date{yyyy-MM-dd HH:mm:ss} - %coloredLevel %logger{15} - %message%n%xException{5}</pattern>
    </encoder>
</appender>

<logger name="play" level="INFO" />
<logger name="application" level="INFO" />

<root level="INFO">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>
<root level="WARN">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>
<root level="ERROR">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>

My usage:

Logger.info("Attached shutdown hook for jedis pool.");

The output:

2014-05-22 15:28:39 - [info] application - Attached shutdown hook for jedis pool.

I wish it was more like:

2014-05-22 15:28:39 - [info] JedisManager - Attached shutdown hook for jedis pool.

How can I accomplish this?

In my application.conf:

application.secret=""
application.langs="en"

play {
  akka {
    loggers = ["akka.event.Logging$DefaultLogger", "akka.event.slf4j.Slf4jLogger"]
    loglevel = WARNING
    actor {
      default-dispatcher = {
        fork-join-executor {
          parallelism-factor = 1.0
          parallelism-max = 36
        }
      }
    }
  }
}

db-context {
    fork-join-executor {
        parallelism-min = 30
        parallelism-max = 30
    }
}

Just define the predefined method in one static class like

public static void error(String data, String className) {
        Logger logger = Logger.getLogger(className);
        logger.error(getClassNameAndLineNo(className)+ data); //log4j.logger

        play.Logger.error(data + getClassNameAndLineNo(className)); //play default logger
    }

and whenever you want to display log,just call the error method like this.

private static final String className =YourClassNameFromWhereYouAreCalling.class.getName();
className.error("msg",className);

because, the method is static method.so you can call by ClassName.error();

It looks like the logger configuration is not picked up. Try the following:

  • make sure the logger configuration is called logger.xml or application-logger.xml and placed in the conf/ directory of the application
  • remove / comment out the logging instructions from conf/application.conf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM