Logback - log catalina in a separate file - logback

I configured TomEE with the Logback-Logging-Framework and I need to log the TomEE/Catalina output to an extra catalina.out file, while generally all the logging goes to an application.log.
I came up with the follwing configuration, but in my catalina.out file is still all the hibernate statements .. how can I separate the logging statements?
How can i log a given package only in a separate file?
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://logback.qos.ch/manual/configuration.html -->
<configuration scan="true" scanPeriod="60 seconds">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator" />
<!-- =============================================== -->
<!-- Logging TomEE/Catalina -->
<!-- =============================================== -->
<appender class="ch.qos.logback.core.FileAppender" name="FILE-CATALINA">
<file>${catalina.base}/logs/catalina.out</file>
<encoder>
<pattern>%d %-5p: [%c] %m %rEx{20} %n</pattern>
</encoder>
</appender>
<logger name="org.apache.catalina" level="INFO" additivity="false">
<appender-ref ref="FILE-CATALINA" />
</logger>
<!-- =============================================== -->
<!-- Logging Application -->
<!-- =============================================== -->
<appender class="ch.qos.logback.core.FileAppender" name="FILE-APP">
<file>${catalina.base}/logs/application.log</file>
<encoder>
<pattern>%d %-5p: [%c] %m %rEx{3} %n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="INFO" />
<logger name="org.apache" level="INFO" />
<logger name="org.quartz" level="INFO" />
<!-- =============================================== -->
<!-- ROOT Logger -->
<!-- =============================================== -->
<root level="ERROR">
<!-- log packages with INFO and all errors -->
<appender-ref ref="FILE-APP" />
</root>
<root level="OFF">
<!-- only log package org.apache.catalina -->
<appender-ref ref="FILE-CATALINA" />
</root>
</configuration>
Now the catalina.out log still has hibernate logs (because hibernate is configured as INFO):
2015-04-30 13:38:24,004 INFO : [org.apache.catalina.startup.Catalina] Initialization processed in 4949 ms
2015-04-30 13:38:24,033 INFO : [**org.apache.catalina**.core.StandardService] Starting service Catalina
2015-04-30 13:38:24,034 INFO : [org.apache.catalina.core.StandardEngine] Starting Servlet Engine: Apache Tomcat (TomEE)/7.0.47 (1.6.0)
2015-04-30 13:38:28,484 INFO : [org.hibernate.annotations.common.Version] HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2015-04-30 13:38:28,506 INFO : [org.hibernate.Version] HHH000412: Hibernate Core {4.2.8.Final}

Just figured it out:
I deleted the second root appender (root level="OFF" ...) and appender FILE-CATALINA gets only the messges from the configured appender-ref (no more root level messages).

Related

Logback - How to get ListAppender instance

I'm using a logging client/server architecture with SocketAppender in the client, and SimpleSocketServer in the server.
I want to store all the logs in a Java List for later processing. I think I can use the ListAppender, which has a backing List collection.
I got the Console and Rolling appenders working, but I have no idea how to get the ListAppender instance so I can access the logs.
Client config file
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ==================================================================== -->
<!-- SocketAppender configuration file. -->
<!-- ==================================================================== -->
<configuration>
<appender name="SOCKET" class="ch.qos.logback.classic.net.SocketAppender">
<RemoteHost>127.0.0.1</RemoteHost>
<Port>6000</Port>
<ReconnectionDelay>5000</ReconnectionDelay>
<IncludeCallerData>true</IncludeCallerData>
</appender>
<root level="debug">
<appender-ref ref="SOCKET" />
</root>
</configuration>
Server config file
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ==================================================================== -->
<!-- This config file is intended to be used by a SocketServer that logs -->
<!-- events received from various clients on the console and to a file -->
<!-- that is rolled over when appropriate. The interesting point to note -->
<!-- is that it is a configuration file like any other. -->
<!-- ==================================================================== -->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date %-5level [%thread] %logger - %message%n</Pattern>
</layout>
</appender>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>rolling.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>rolling.%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>3</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>8KB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%relative %-5level %logger - %message%n</Pattern>
</layout>
</appender>
<appender name="LIST" class="ch.qos.logback.core.read.ListAppender"/>
<root>
<level value ="debug"/>
<appender-ref ref="CONSOLE" />
<appender-ref ref="ROLLING" />
</root>
</configuration>
In order to use ListAppender, I was missing to attach it to the root logger in the server xml file. Check the root tag in the server config file below.
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ==================================================================== -->
<!-- This config file is intended to be used by a SocketServer that logs -->
<!-- events received from various clients on the console and to a file -->
<!-- that is rolled over when appropriate. The interesting point to note -->
<!-- is that it is a configuration file like any other. -->
<!-- ==================================================================== -->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date %-5level [%thread] %logger - %message%n</Pattern>
</layout>
</appender>
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>rolling.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>rolling.%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>3</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>8KB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%relative %-5level %logger - %message%n</Pattern>
</layout>
</appender>
<appender name="LIST" class="ch.qos.logback.core.read.ListAppender"/>
<root>
<level value ="debug"/>
<!-- <appender-ref ref="CONSOLE" />-->
<!-- <appender-ref ref="ROLLING" />-->
<appender-ref ref="LIST" />
</root>
</configuration>
After that, it's trivial to get the list appender in Java:
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.LoggerFactory;
Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) rootLogger.getAppender("LIST");

logs disappear in cloud run once I start using google's LoggingAppender in logback?

I have no idea how to debug since there is no errors. I have this logback file but this made all the logs disappear for my cloudrun instance EXCEPT the system.out stuff that logback logs on startup..
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="30 seconds" debug="true">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<appender name="CLOUD" class="com.google.cloud.logging.logback.LoggingAppender">
<!-- Optional : filter logs at or above a level -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->
</appender>
<appender name="ASYNC-SERVERLOG" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="CLOUD" />
</appender>
<!-- TBD for later use for apps that use log4jdbc it doesn't hurt apps that
don't use it -->
<logger name="jdbc.sqlonly" level="INFO" />
<logger name="jdbc.sqltiming" level="WARN" />
<logger name="jdbc.audit" level="WARN" />
<logger name="jdbc.resultset" level="WARN" />
<logger name="jdbc.connection" level="WARN" />
<root>
<level value="INFO" />
<appender-ref ref="ASYNC-SERVERLOG" />
</root>
</configuration>
Any ideas why logging stops working or where the logging is going to? Now that I changed logging to this, logging seems to go to the void :(.
I also tried out this post which logs to ConsoleAppender but NOTHING shows up in the json format although it is doing a line for EACH log statement I do which is really nice
GKE & Stackdriver: Java logback logging format?
I then tried this approach which is using FileAppender
How do I map my java app logging events to corresponding cloud logging event levels in GCP Felexible non-compat App Engine?
My logback for this one was
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/var/log/app.log</file>
<append>true</append>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.orderlyhealth.GCPCloudLoggingJSONLayout">
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg</pattern>
</layout>
</encoder>
</appender>
<appender name="ASYNC-SERVERLOG" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
Any ideas?
The Java logging instructions in the documentation should help: https://cloud.google.com/run/docs/logging#run_manual_logging-java
In Cloud Run, output to Stdout and Stderr are forwarded on to Stackdriver. Logs which are recorded in files are lost.
I'm not sure the implications of the ASYNC-SERVERLOG appender, but on Cloud Run there is only CPU available to process and send logs while a request is being processed, so if something is asynchronously sending logs it will only work during that request or a future request that reaches the same container instance.

Logback log level change not working

I'm using an application called Apache Drill that uses logback for logging. I'm trying to edit it's shipped logback.xml configuration file to output debugging messages.
Here is the file, untouched
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration>
<!-- <appender name="SOCKET"
class="de.huxhorn.lilith.logback.appender.ClassicMultiplexSocketAppender">
<Compressing>true</Compressing>
<ReconnectionDelay>10000</ReconnectionDelay>
<IncludeCallerData>true</IncludeCallerData>
<RemoteHosts>${LILITH_HOSTNAME:-localhost}</RemoteHosts>
</appender>
-->
<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>
<appender name="QUERY" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.query.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${log.query.path}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${log.path}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.apache.drill" additivity="false">
<level value="info" />
<appender-ref ref="FILE" />
</logger>
<logger name="query.logger" additivity="false">
<level value="info" />
<appender-ref ref="QUERY" />
<!-- <appender-ref ref="SOCKET" /> -->
</logger>
<!--
<logger name="org.apache.drill" additivity="false">
<level value="debug" />
<appender-ref ref="SOCKET" />
</logger>
-->
<root>
<level value="error" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
All I did afterwards was editing
<logger name="org.apache.drill" additivity="false">
<level value="info" />
<appender-ref ref="FILE" />
</logger>
to be
<logger name="org.apache.drill" additivity="false">
<level value="debug" />
<appender-ref ref="FILE" />
</logger>
And absolutely nothing changed !
This is the java command used to start the application (Search for -cp /home/gelbana/drillv11/conf, the logback.xml file is located in the conf directory)
/usr/java/jdk1.8.0_112/bin/java -Xms10G -Xmx10G -XX:MaxDirectMemorySize=230G -XX:ReservedCodeCacheSize=1G -Ddrill.exec.enable-epoll=false -XX:MaxPermSize=512M -Djava.io.tmpdir=/home/gelbana/drillv11/tmp -Ddrill.memory.debug.allocator=true -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -Dlog.path=/home/gelbana/drillv11/log/drillbit.log -Dlog.query.path=/home/gelbana/drillv11/log/drillbit_queries.json -cp /home/gelbana/drillv11/conf:/home/gelbana/drillv11/jars/*:/home/gelbana/drillv11/jars/ext/*:/home/gelbana/drillv11/jars/3rdparty/*:/home/gelbana/drillv11/jars/classb/* org.apache.drill.exec.server.Drillbit
The drillbit.out file has the following lines at the beginning
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/gelbana/drillv11/jars/3rdparty/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/gelbana/drillv11/jars/classb/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
I tried changing all levels to DEBUG, ALL but still, nothing changed. I can't find a single DBEUG in the logs.
The changes you made should work. I made the same change and ran a drillbit and I can see debug logs in drillbit.log. For example:
DEBUG o.a.drill.exec.server.StartupOptions - Parsing arguments.
The drillbit.out file should tell you something about how Logback configured itself. Have a look at that file, specifically the entries which relate to LoggerContext for example:
Found resource [logback.xml] at ...
Resource [logback.xml] occurs multiple times on the classpath
And you'll likely find that Logback relveals the answer via it's own logging.

Package-specific logging levels for different Logback appenders

I have this simple Logback config file, containing two appenders and some custom logging levels based on package name.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%date{HH:mm}\t%-5level\t%msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>my_logger.log</file>
<encoder>
<charset>UTF-8</charset>
<pattern>%date{dd MMM HH:mm}|%class{0}|%-5level|%msg%n</pattern>
</encoder>
</appender>
<!-- custom logging levels -->
<logger name="myapp.package1" level="INFO" />
<logger name="myapp.package2" level="INFO" />
<root>
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
With this configuration, the custom logging levels are applied for both appenders.
How can I change it so that only ConsoleAppender uses these custom logging levels, while FileAppender sticks to default levels?
If you don't want to implement a custom filter, you can create a new appender with a fixed threshold (in you case INFO):
<appender name="INFO_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
...
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
Then, for your custom logging levels add the INFO_CONSOLE and your FILE appender. The additivity="false" attribute prevents the logger from logging to the CONSOLE appender inherited from root.
<logger name="myapp.package1" additivity="false">
<appender-ref ref="INFO_CONSOLE" />
<appender-ref ref="FILE" />
</logger>
This should log DEBUG and above to the FILE and CONSOLE appenders, except for myapp.package1 which will only log INFO and above to CONSOLE.

Root Level Logging is effective in service mix

I am using slf4j & logback for logging and my application is deployed in Apache Service Mix where other modules are using my logging service. Everything works fine when i test it in a standalone environment but i face problem when i deploy it in a service mix container. At that time, only root level logging is effective and other logging levels are ignored.
Below is my logback-test.xml configuration, Kindly help i am stuck in this issue for the last 4 days.
<!-- This property describes the location of the property file. -->
<property
file="C:/Users/evikdew/ccl_code/log.properties" />
<!-- This appender prints on the console. -->
<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>
<!-- This appender contains the properties for the logs that would be written
to a file. -->
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIRECTORY}\${LOG_FILE_NAME}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE_PATTERN}</fileNamePattern>
<maxHistory>${MAX_LOG_HISTORY}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${LOGGING_PATTERN}
</pattern>
</encoder>
</appender>
<logger name="com.ericsson" level="Error" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="com.ericsson" level="INFO" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="com.ericsson" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
As Servicemix 4.x is based on top of Karaf, the standard logging mechanism is Pax Logging. Pax Logging is configured by the org.ops4j.pax.logging.cfg file, as Karaf uses Configuration Admin Service to configure OSGi services. That's why the config file still uses the log4j properties notation.
If you need special appenders, some are already included like a MDC appender for logging bundle wise, or a ZipRollingFileappender.
Support for the external logback configuration was introduced with Pax Loggin 1.7 I think, and I doubt it is already included in ServiceMix 4.5.x
So you have to stick to the log4j notation or exchange the Pax Logging versions yourself.