AppEngine - Log4j and Log-Levels - java

I want to use Log4j for my logging on AppEngine. I configured the logger like this:
.level = INFO
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{1}:%M:%L - %m%n
But the logger then logs everything to INFO and the real log level is just shown inline like this: (Log level error)
Is there a way to get the real log levels to work with log4j so that filtering on log levels works on the appengine UI?
Edit: More detailed example:
So I log an error like this:
import org.apache.log4j.Logger;
private static Logger logger = Logger.getLogger(PingServlet.class);
logger.error("Database was successfully pinged.");
And on the Appengine-UI, it shows up as an INFO (the green I). Only the loggin text indicates the correct log level:

The GAE Java documentation states that everything you write to stdout is logged as INFO and to stderr is WARNING [1]. Your root logger is writing everything to stdout.
I'm not a log4j expert, so I don't know of a way for a logger to write to both stdout and stderr. One workaround is to use two loggers, one for INFO and another one for WARNING.
[1] https://cloud.google.com/appengine/docs/java/requests#Java_Logging

Related

How to set up a log4j.properties file for sub packages

I want to create log file for two packages: net.biomodels.jummp.indexing and net.biomodels.jummp.indexing.solrindexer. However, it does not work as I expect. The log only contains what are involving in net.biomodels.jummp.indexing package.
Could you help me finding out the missing of the following logging configuration?
log4j.rootLogger=WARN, R
# everything goes to the general log
log4j.logger.net.biomodels.jummp.indexing=DEBUG, stdout, R
log4j.additivity.net.biomodels.jummp.indexing=false
# I want to log classes in solrindexer package, underneath indexing package
log4j.logger.net.biomodels.jummp.indexing.solrindexer=INFO, stdout, R
log4j.additivity.net.biomodels.jummp.indexing.solrindexer=false
## general log
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/general.log
log4j.appender.R.MaxFileSize=1MB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p %t %d{ISO8601} %c{2} - %m%n
Your configuration is fine from a syntax point of view. I suspect your log level is the problem. You have your first logger set to DEBUG level:
log4j.logger.net.biomodels.jummp.indexing=DEBUG, stdout, R
while your second logger is set to INFO level:
log4j.logger.net.biomodels.jummp.indexing.solrindexer=INFO, stdout, R
Thus if you are expecting to see DEBUG level logs from classes in the package net.biomodels.jummp.indexing.solrindexer you will not see them unless you change the log level of your logger to DEBUG or lower.

How to stop Hibernate from spamming the logs

I have hibernate running on app-engine and hibernate logs so much debug infos to the console/log (see attached screenshot) and I can't figure out how to stop it.
My log4j.properties looks like that:
# Root logger option
log4j.rootLogger=WARNING, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.logger.org.hibernate=WARNING
So this normally should only log warnings right?
My logging.propertis looks like that (But I guess hibernate does not care about that because it uses log4j):
java.util.logging.ConsoleHandler.level = SEVERE
In Log4J the keyword is WARN not WARNING. Thats Java Util Logging. See here.

Log4j on AppEngina -> All log messages shown with the same log level

My log messages always show up as "info" in the app engine logging console, even if i log a message as error (see screenshot).
I log a message like that:
private static Logger logger = Logger.getLogger(PingServlet.class);
logger.error("Database was successfully pinged!!.");
The configs like this:
src/main/resources/log4j.properties
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
src/main/webapp/WEB-INF/logging.properties
# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
#
# <system-properties>
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#
# Set the default logging level for all loggers to WARNING
.level = INFO
How can I change that? I want to show an error as error and so on.
If you are logging with log4j the log messages are being printed to the "console" with ConsoleAppender which AppEngina then collects in its own log. The two log parts you have are not connected.
you either need to use java.util.logging directly instead of log4j or use slf4 to redirect log4j to java.util.logging (a bit tricky, read the documentation carefully)

Shunting certain org.apache.log4j log messages off to a particular log file

We already have extensive logging in our application using org.apache.log4j. We now want to shunt some of these messages off to a new XML log file (while continuing to go to the original log file).
Is this possible? Is there a way we can identify these messages and send them someplace special in addition to the regular log file?
I think you can define two file appenders. One at the root level and other at the package(select the package level as appropriate) level as below:
# Root logger option
log4j.rootLogger=DEBUG, RootFileAppender
#Shunted Logger option
log4j.logger.com.shunted=ERROR,ShuntedFileAppender
# RootFileAppender - used to log messages in the root.log file.
log4j.appender.RootFileAppender=org.apache.log4j.FileAppender
log4j.appender.RootFileAppender.File=root.log
log4j.appender.RootFileAppender.MaxFileSize=100MB
log4j.appender.RootFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
# ShuntedFileAppender - used to log messages in the shunted.log file.
log4j.appender.ShuntedFileAppender=org.apache.log4j.FileAppender
log4j.appender.ShuntedFileAppender.File=shunted.log
log4j.appender.ShuntedFileAppender.MaxFileSize=10MB
log4j.appender.ShuntedFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ShuntedFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
Please Note: You can define the two logger option as different levels as well. In above example, ROOT is defined at DEBUG while shunted is devined as ERROR level.

Set differnet levels of logging for loggers in log4j

I am using log4j for my application - there I need to see all log messages, so I defined
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
But, after I started using Apache's HTTPClient library (which is awesome btw), my console got clouded up by its logging - everything was logged and I am not able to see the log output from my own application any more.
So how can I tell the HTTPClient library that it should only log WARN messages, while my own application still logs on ALL levels?
I create my application logger using
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
It's quite simple. Just add the below line.
log4j.logger.org.apache.commons.httpclient=WARN
Similarly, you can configure levels for any package com.foo or class com.foo.Bar by appending it to the prefix log4j.logger as follows:
log4j.logger.com.foo=MYLEVEL
log4j.logger.com.foo.Bar=DEBUG

Categories