Bizarre "18 chars" LOGBack behaviour - java

This is my first time using Logback - normally use log4j - been forced into it by a dependency.
I used the online converter for log4j.properties --> logback.xml.
When I run the application, all I get are the first 18 chars of any log line, without a newline at the end. The resulting output, both on the console and in the log file is:
16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main16:32:00.537
(main16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main16:32:00.537 (main
What on earth could possibly be causing this?
The logback.xml is
<configuration>
<appender name="xyzzy" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>xyzzy.log</File>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} (%t) %-5p [%c{36}] - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>xyzzy.log.%d</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} (%t) [%c{36}] %msg%n</pattern>
</encoder>
</appender>
<logger name="com.package" level="DEBUG"/>
<logger name="org.apache.http.client.protocol.ResponseProcessCookies" level="ERROR"/>
<root level="WARN">
<appender-ref ref="xyzzy"/>
<appender-ref ref="console"/>
</root>
</configuration>

I don't know LOGBack but, according to the documentation,
I'm pretty sure that
(%t)
should be
[%t]
and it is exactly the point where your pattern breaks...
Try then with:
<pattern>%d{HH:mm:ss.SSS} [%t] %-5p [%c{36}] - %msg%n</pattern>
[...]
<pattern>%d{HH:mm:ss.SSS} [%t] [%c{36}] %msg%n</pattern>
assuming all the rest is fine...

In logback, parenthesis in within the pattern string serve as grouping tokens. They need to be escaped.

Related

Spring Boot console color

When i run the springboot application i get the console log color in red. I have tried to change the colors by addingspring.output.ansi.enabled=always in the application.properties and added a logback.xml file as well, but it didn't work.
this is my logback.xml file:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
this is the console color
what did i miss? any suggestions ?
When you add logback.xml is not enough just to set spring.output.ansi.enabled=ALWAYS but also need to use the %clr conversion word in your logging pattern. Example, if the logging pattern was previously logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n now it could instead be entirely colored as logging.pattern.console=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level - %msg%n) or to color just the log-level in console logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} $clr(%-5level) - %msg%n
Here is example of logback.xml file that will give colored output:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- On Windows machines setting withJansi to true enables ANSI
color code interpretation by the Jansi library. This requires
org.fusesource.jansi:jansi:1.8 on the class path. Note that
Unix-based operating systems such as Linux and Mac OS X
support ANSI color codes by default. -->
<withJansi>true</withJansi>
<encoder>
<pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

logback line number is not showing up in logs

I am using logback 1.2.3 . My configuration looks like below
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target>
<encoder>
<pattern>%p [%d{yyyy-MM-dd HH:mm:ss,SSS}] %c{3}:[%C{1}:%M:%L] - [%t] - %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
But I am not seeing the line number in my logs
INFO [2018-01-12 07:06:54] c.v.s.f.FlowStoreWarmer:[?:?:?] - [FlowStoreWarmer_flow_cache_warmup_thread] - no work to do for cid 19099
Can some one let me know what is going wrong?
Change your encoder as below:
<encoder>
<pattern>%p [%d{yyyy-MM-dd HH:mm:ss,SSS}] %c{3}:[%C{1}:%M:%L] - [%t][%file:%line] - %m%n</pattern>
</encoder>
[%file:%line] means show source file name and line number.

How can I configure logback conf to send all messages to stderr?

Recently added some println output to my program...
and now dont want logback INFO messages merging with them.
How can I get all messages to stderr ?
Create appender and attach all logs for it:
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<target>System.err</target>
<encoder>
<pattern>%date [%thread] - 5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache" level="INFO ">
<appender-ref ref="STDERR"/>
</logger>
All org.apache class logs(that uses slf4j) will be directed to System.err
Use a ConsoleAppender with the target attribute set to System.err. See http://logback.qos.ch/manual/appenders.html#ConsoleAppender for details.
Something like this in your logback.xml should work:
<configuration>
<appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<target>System.err</target>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDERR" />
</root>
</configuration>

Logback's debug output not shown

I'm running a Java servlet inside Tomcat 7.0.52.
This is my logback.xml file
<configuration debug="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Unfortunately I'm nowhere seeing Logback's debug output. Neither in catalina.out, nor in my-log.log or on the console. Where is it?
Just to add: Changes in logback.xml are reflected in the logging output.
In your logback configuration, you define a file appender. What you do not do is define a ConsoleAppender, which will log to stdout/stderr.
catalina.out being the redirection of stdout/stderr used by the Tomcat init script, adding this to your configuration should solve your problem.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern><![CDATA[
%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80}[%L] - %msg%n
]]></pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE" />
</root>

Defining 1 logback file per level [duplicate]

This question already has answers here:
How can I configure Logback to log different levels for a logger to different destinations?
(12 answers)
Closed 6 years ago.
I'd like to define a logback configuration file that will be able to :
log DEBUG (and higher) things in debug.log
log INFO (and higher) things in info.log
That is to say, when I call :
logger.warn("blah"); // Logs to both info.log and debug.log
logger.debug("bleh"); // Logs to debug.log only
Is it possible ?
I tried something like this, but it seems like when logback encounters multiple <root> tags, it only takes the last one :
<appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/info.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="debugFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/debug.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="infoFile" />
</root>
<root level="DEBUG">
<appender-ref ref="debugFile" />
</root>
Here, info.log contains debug-level logs :(
Note that I want to apply this behaviour on every of my packages.
Using a levelFilter solved the problem
Example :
<appender name="infoFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/info.log</File>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="debugFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGS_FOLDER}/debug.log</File>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG"><!-- Using the lowest level here -->
<appender-ref ref="debugFile" />
<appender-ref ref="infoFile" />
</root>

Categories