How to make Playframework Evolutions logging more verbose? - playframework-2.5

I've been trying to see what's going on with them, as I'm facing a problem.
When I click the "apply this script" button, the page asking me to apply the script appears again, and it stucks in that loop. It creates the "play_evolutions" table, but no script is run.
The log in the console doesn't give me any relevant info. I've run manually the script and it's ok... So I wan't to know what the real problem is, therefore I need more logging.

Evolutions are not very verbose in general (when it talks it mostly does that on warn level).
Basically make sure you have this line at you should get everything play-evolutions produce (it should be in logback.xml file):
<logger name="play.api.db.evolutions" level="DEBUG" />
Full file (as an example only):
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%level %logger{15} - %message%n%xException</pattern>
</encoder>
</appender>
<logger name="play.api.db.evolutions" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

Related

Logback configuration issue

I'm using Spring Boot 2.3.2. I'm trying to configure logback to continue outputing to the console EXACTLY how it does today (ansi and all), plus mirror it to a log file in the exact same format (ansi and all). I want ALL the log files to be in the format of ${spring.application.name}.YYYY-MM-DD.log including the active one and I want to keep 7 days worth of files, so I've got it configured like this:
application.properties:
logging.file.name=${spring.application.name}
logging.file.path=/var/logs
logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<timestamp key="byDate" datePattern="yyyy-MM-dd" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}.${byDate}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
This results in a collision error between file and fileNamePattern. I get what the error means and I can fix it by removing the date stamp from file, but there is no way to have all the files in a consistent format?
If I remove the timestamp from the active file, then it works except for the next issue...
The other issue that I have is that the logfile never gets written too until I shutdown the application.
This results in a collision error between file and fileNamePattern. I
get what the error means and I can fix it by removing the date stamp
from file, but there is no way to have all the files in a consistent
format?
Here :
<fileNamePattern>${LOG_PATH}/${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
you don't have to specify ${LOG_PATH}. It is a filename pattern not a full path.
That should be that :
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
The other issue that I have is that the logfile never gets written too
until I shutdown the application.
Not sure but it looks a file locking. Try to ensure that no process locks the file.
As an additional remark : you specified :
logging.file.name=${spring.application.name}
logging.file.path=/var/logs
But the spring boot logging doc specify to reference the one or the other :
you need to set a logging.file.name or logging.file.path property (for
example, in your application.properties).
Valuing logging.file.name such as the following respects better the documentation :
logging.file.name=/var/log/${spring.application.name}

SpringBoot with LogBack creating LOG_FILE_IS_UNDEFINED folder

I am using SpringBoot with LogBack and I am trying to direct all log-statements of one specific package (here shown as "com.example.somepackagename") to a file. All other log statements should go to stdout.
At first ran into the issue that during startup a file "LOG_FILE_IS_UNDEFINED" was created. I then googled on that issue (found e.g. this on github or this on StackOverflow and a few others), but none of the appends really solved the issue.
Based on the misc. descriptions I came up with the below configuration using a logback-spring.xml-file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_PATH" value="${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}" />
<property name="LOG_FILE" value="${LOG_FILE:-ts-msgs.log}" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="MSG_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/${LOG_FILE}</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n%wex</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_PATH}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.example.somepackagename" additivity="false" level="debug">
<appender-ref ref="MSG_LOG_FILE" />
</logger>
<logger name="org.springframework" additivity="false" level="info">
<appender-ref ref="STDOUT" />
</logger>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
With that I don't get a file called "LOG_FILE_IS_UNDEFINED" anymore but LogBack now always first creates an empty file "ts-msgs.log" in the working directory of the application before it creates a file with the same name in the temp-directory (where that file should go). So, somehow during initialization it already creates a file with the correct name but the correct path not being set, yet. In other words, the initialization order of Logback within SpringBoot seems broken (or at least not intuitive) and it seems impossible to avoid a first log-file being created before the correct path for the file-appender is being defined and applied.
How can one prevent that first, initial and empty log file from being created? This is necessary, since the working directory is in general write-protected and the attempt to create a file there will lead to an exception (and typically the program being terminated).
Any hints or suggestions?
BTW: I already tried to rename that file to logback.xml (which seems to be processed earlier during SpringBoot startup) but that didn't solve the issue, so I kept the suggested filename.
I get similar issue, on application start following empty file might be generated:
LOG_PATH_IS_UNDEFINED
LOG_FILE_IS_UNDEFINED
Reason
Logging related config in application.yml, is read after logback-spring.xml is parsed, thus can't read it.
Possible solutions
I found 3 possible solutions, choose one that best fit your case:
Move logging config from application.yml to bootstrap.yml, this will need spring-cloud dependency to make it work.
This works because bootstrap.yml is read before logback-spring.xml.
If you are not using spring-cloud, this probably won't be your best choice, since the extra dependencies is unnecessary.
Define path & file in logback-spring.xml directly.
e.g
<configuration>
<springProperty name="LOG_PATH" source="logging.path" defaultValue="logs/" />
<springProperty name="LOG_FILE" source="logging.file" defaultValue="app.log" />
In this case, might need to add "logback-spring.xml" to each sub project, if the log file names need to be different, and can't simply put the config file in a shared common dependency.
Just keep the config in application.yml, and ignore the generated empty files by setting .gitinore.
e.g
LOG_*_IS_UNDEFINED
In this case, the logs are still written to the file specified by application.yml, though the empty file is generate.
Tips
The logback-spring.xml file mentioned above, might be logback.xml or some other name in your case.
Removing :
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
from logback.xml fixed the problem for me.

Setting level to a logger in spring-boot

In logback-spring.xml file of a project that is new to me I see this configuration
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
......
</appender>
<logger name="com.myproject.myclass" level="${APP_LOGGING_LEVEL:-INFO}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
As far as I understand, all logs from com.myproject.myclass class will be sent to <appender name="STDOUT"> that is console.
Could someone explain me what will be the level of this log with this config level="${APP_LOGGING_LEVEL:-INFO}"? In documentation I see only constants like WARN, DEBUG, etc
In Spring ${APP_LOGGING_LEVEL:-INFO} means APP_LOGGING_LEVEL is a dynamic variable. The value for which can be set from properties file or from command line when running the project or from any other place accessible. If not set whatever after : is selected.
In your case if not set INFO is selected

Spring - Exceptions do not log to a file

I'm currently using SLF4J API for logging.
Whenever an exception is thrown during runtime, the full error stack trace does not log to file, it is only printed to the console. I'm using eclipse.
Here is my code for logback.xml (currently located in classes folder under WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<!-- Specify here the path of the folder you want to save your logs -->
<property name="LOGFILE_PATH" value="C:/Logs" />
<!-- All logging will be redirected/ printed to console. -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger{50} - %rEx %msg%n </Pattern>
</layout>
</appender>
<!-- Send log to file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGFILE_PATH}/spring-mybatis-log.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd hh:mm:ss a} [%thread] %-5level %logger - %rEx %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGFILE_PATH}/spring-mybatis-log-%d{yyyy-MM-dd}-%i.txt
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Is there something missing/wrong with the above file??
Is it possible to log (to a file) all the text that will be printed to console?
How does spring(or the project itself) read the logback.xml file? What if I rename it and place it in another folder?
How to create one root containing all the levels (INFO, DEBUG, ERROR, WARN, etc..) ?
To answer your questions:
Nothing looks obviously wrong to me in the file you've posted, although I didn't try actually running it.
Just the way you did it is fine, with two appenders, one that goes to the file and the other to go to the console.
Logback by default looks in the classpath for the logback.xml file. Refer to the configuration page of the manual for the details. The way it gets there depends on your build system. When using Maven, I'd suggest putting it in src/main/resources. But if it ends up in WEB-INF/classes when deployed in your web app, that should work. If no matter what you put in your logback.xml file you are only getting console output (try adding a syntax error or renaming the file to test), that's what I'd look at first, to ensure that Logback is picking up the file right. It will default to showing everything just on the console if it can't find the file, though I think it shows a warning at the beginning that it's doing so. If it is picking up the file, you can try putting debug="true" in the <configuration> element, to see if there's an error that it's picking up that's causing it to not use the appender the way you're expecting.
Specifying the "DEBUG" level of logging, as you've done, will also get all higher levels as well.
If the issue is that logging from Spring isn't going where you want, while your application's logging is working fine, you may need to redirect Spring (which uses Apache Commons Logging) to use SLF4J instead. To do that, remove the commons-logging dependency and add the jcl-over-slf4j library. This will emulate the commons-logging calls and have them point to SLF4J instead. See the "Using SLF4J" section of this blog post for more details.
Your configuration looks OK, but you can try use your root level INFO instead DEBUG and if you have frameworks like spring, hibernate etc. Logback allow you uses another levels to them something like:
<logger name="org.hibernate" level="OFF" />
<logger name="org.springframework" level="INFO" />
<logger name="org.springframework.web.servlet.mvc" level="INFO" />

Logging with slf4j, and 'logback', but not creating specified log file which is in configuration. (using maven, jetty)

As specified in title I'm using Maven, and Jetty. For logging using SLF4J and Logback. I have 'logback.xml' at 'src/main/resources'.
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.FileAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%-4relative [%thread] %-5level %class - %msg%n</pattern>
</layout>
<File>myLog.log</File>
</appender>
<logger name="org.mortbay">
<level value="debug" />
</logger>
<root>
<level value="error" />
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
But my problem is its not creating the file 'myLog.log' if I run/debug the project. What's the solution to get the log file.
Is there any way to get the log file only with SLF4J?
Sorry! I misunderstood the usage of 'Logback'. I got solution from http://www.mail-archive.com/user#slf4j.org/msg00661.html
i.e.
It appears that you have misunderstood
the purpose of SLF4J. If you place
slf4j-jdk14-1.5.6.jar then slf4j-api
will bind with java.util.logging.
Logback will not be used. Only if you
place logback-core.jar and
logback-classic.jar on your class path
(but not slf4j-jdk14-1.5.6.jar) will
SLF4J API bind with logback. SLF4J
binds with one and only one underlying
logging API (per JVM launch).
HTH,
Thanks to Ceki Gulcu. Now I can able to get logs in my file.
If you are using JBoss 5.1 and you are having the same problem[logback not writing to file] then add the following in jboss-web.xml.
<class-loading>
<loader-repository>
com.hp:classloader=logback-slf4j
<loader-repository-config>java2ParentDelegation=false</loader-repository-config>
</loader-repository>
</class-loading>
This should solve your problem.

Resources