I've a simple scala app (not using Play MVC framework) but that uses play.api.Logger
I'm trying to figure out the pattern I need to add to include the File and Line where the log was done.
This was the logback.xml I was using from the start:
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date %-16coloredLevel %message %n</pattern>
</encoder>
</appender>
<logger name="play" level="INFO"/>
<logger name="application" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
and it would display something like:
2016-02-22 19:20:05,901 [debug] MY-LOG-MESSAGE
So I tried to change using the docs, and I came to something like
<pattern>%date %-16coloredLevel %F L.%L:: %message %n</pattern>
that produced
2016-02-22 22:26:49,725 [debug] Logger.scala L.74:: MY-LOG-MESSAGE
It is reporting the File as the play.api.Logger class file and the corresponding line.
What do I need to write to I could get something like com.package.Clazz.scala L.10
?
EDIT
Output for marcospereira answer:
Got something like:
2016-02-23 11:39:36,624 [info] play.api.LoggerLike$class L.93:: MESSAGE
What you need is well documented as Logback log layouts:
c{length}
, lo{length}
or logger{length}
Outputs the name of the logger at the origin of the logging event.
This conversion word takes an integer as its first and only option. The converter's abbreviation algorithm will shorten the logger name, usually without significant loss of meaning. Setting the value of length option to zero constitutes an exception. It will cause the conversion word to return the sub-string right to the rightmost dot character in the logger name.
L
OR line
:
Outputs the line number from where the logging request was issued.
Generating the line number information is not particularly fast. Thus, its use should be avoided unless execution speed is not an issue.
So, what you need to do is edit your conf/logback.xml
file to alter the log pattern like this:
<pattern>%date %-16coloredLevel %logger L.%L:: %message %n</pattern>
Notice the %logger
part of the log pattern.
A possible problem here is that, if you are using the logger helper (play.api.Logger) directly (Logger.info, per instance), the the class will be play.api.LoggerLike
, which is the class for which the log was created. Of course, this is not useful for your use case, so you can use the log like this:
Scala:
import play.api.Logger
import play.api.mvc.Controller
val logger = Logger(this.getClass)
logger.info("Logging a message")
Java:
import play.Logger;
import play.Logger.ALogger;
class Something {
private static final ALogger logger = Logger.of(Something.class);
public void whatever() {
...
logger.info("some message");
...
}
}