0

I've been troubleshooting this error for hours now, so I decided to ask it on Stackoverflow. For a project I need to add logging, and I chose Logback due to its functionality.

I added logback-classic and logback-access (for TomEE) to my pom.xml and added logging statements to a class to test it out. In my console, I can see that there are events being logged, but they don't get written to a .log file. This likely indicates that TomEE uses the default Logback configuration, and not the one I set up as follows (under resources/WEB-INF/classes/logback.xml):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>myApp-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="trace">
        <appender-ref ref="FILE"/>
    </root>
</configuration>

If I run my Maven unit tests, Maven seems to pick up this configuration and Logback correctly writes some events to the log file. But if I launch my Artifact on IntelliJ, nothing seems to happen (it only prints events to the console, but not to a log file). I am not sure where the issue lies here, it could be either IntelliJ or TomEE. I hope someone can lend me a hand here.

  • I tried adding -Dlogback.configurationFile= to the VM options in IntelliJ
  • I checked the target directory and it shows the logback.xml in its correct place:

  • I've reimported the project in IntelliJ by removing the .idea folder
  • I've updated tomEE and IntelliJ
RubenF
  • 11
  • 2
  • > "But if I launch my Artifact on IntelliJ, nothing seems to happen" What exactly do you mean by that? – CrazyCoder May 03 '23 at 19:44
  • Thanks for replying. I should have worded my question a bit better. I can see the event in the console, but not in a .log file. Only a maven test can append the file. – RubenF May 03 '23 at 19:54

2 Answers2

0

Are you saying it's appending to the file correctly, but not the IntelliJ console? You need another appender below your file appender (adapted from the relevant documentation)

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <!-- ADD FORMATTING YOU WANT -->
  <encoder>
    <pattern>%-4relative [%thread] %-5level %logger{35} -%kvp- %msg %n</pattern>
  </encoder>
</appender>

Then something like this, adapted from this answer:

<logger name="fileLogger" level="TRACE">
    <appender-ref ref="FILE"/>
</logger>

<root level="TRACE">
    <appender-ref ref="STDOUT"/>
</root>
kay
  • 46
  • 4
  • 1
    Thanks for replying. I should have worded my question a bit better. I can see the event in the console, but not in a .log file. Only a maven test can append the file. – RubenF May 03 '23 at 19:53
  • Could it actually be logging to the Tomcat home directory as in [this answer](https://stackoverflow.com/a/73250919/12156111)? (The context isn't given in that answer, but you'd have to add `logFile.log` before your appender). Maven defaults to current directory but Tomcat doesn't – kay May 03 '23 at 20:23
  • I have not been able to locate any log files in any of the folders. I will attempt to give the file an absolute path. However, I have noticed that Logback continues to print logs to the console. I am uncertain whether this is expected behavior, as I have configured it to write logs to a file. – RubenF May 03 '23 at 20:37
0

I abandoned Logback and switched to Log4j2, which is working perfectly fine. Therefore, it seems like there might be an issue with Logback for my repository. Thank you all for your assistance.

RubenF
  • 11
  • 2