2

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

Community
  • 1
  • 1
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82

1 Answers1

1

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");
        ...
    }
}
marcospereira
  • 12,045
  • 3
  • 46
  • 52
  • already tried that one also. Got something like: `2016-02-23 11:39:36,624 [info] play.api.LoggerLike$class L.93:: MESSAGE` – pedrorijo91 Feb 23 '16 at 11:39
  • actually it didn't seem to solve anything. Also, if instead of a controller class the log is done inside an Object, the logger Logger.apply won't work – pedrorijo91 Feb 27 '16 at 10:33
  • What do you mean by "didn't solve anything"? What are you seeing? About using the logger inside an `object`, I've made the Scala example more generic and now it should work with `object`s. – marcospereira Feb 27 '16 at 12:54
  • I still got the same class on the log: `2016-02-27 13:18:12,382 [info] play.api.LoggerLike$class L.96:: Logging a message` is it working on your code? – pedrorijo91 Feb 27 '16 at 13:19
  • Play code using will generate this log if it is using `play.Logger.info`, per instance. You can only **change your own code** to log as explained at my answer. Did you replaced `play.Logger.info` in your code to use what I've explained in my answer? – marcospereira Feb 27 '16 at 13:21
  • yes, used your own code. here's a gist of what I'm using https://gist.github.com/pedrorijo91/c0f7786cb760d9dd086d – pedrorijo91 Feb 27 '16 at 13:26
  • 1
    This is working perfectly to me: https://gist.github.com/marcospereira/bd99429a4ee8811ae8b3. Which version of Play are you using? – marcospereira Feb 27 '16 at 13:30
  • ok, the pattern I'm using (from your answer) does not produce the output correctly. But the pattern from the gist does. Final pattern that works: `%date %-16coloredLevel %logger L.%L:: %message %n`. So it's not `%class` but `%logger`. Thanks a lot, can you fix the answer so I can accept it? – pedrorijo91 Feb 27 '16 at 13:35
  • Both class and logger are working for me. Which version of Play are you using? I've tested with 2.4.x version. – marcospereira Feb 27 '16 at 13:37
  • Not play itself, just playWS: `"com.typesafe.play" %% "play-ws" % "2.4.3"` – pedrorijo91 Feb 27 '16 at 13:41
  • 1
    See answer update. I changed to use logger instead. ;-) – marcospereira Feb 27 '16 at 13:48
  • `%L` and `%F` don't work with play2.5, output as `?` instead – Vasily802 Jan 13 '17 at 23:09