I am using slf4j with log4j, and for some reason all the logs are showing except the logs that I write in my code - Spring logs, Hibernate logs, you name it logs are showing and in the level configured in log4j.properties. My logs however, are not showing.
Maven dependencies:
log4j
log4j
1.2.17
test
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
When running with -Dlog4j.debug I see that my log4j.propertes file is being used
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader#11a5ee7c.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader#11a5ee7c class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader#11a5ee7c.
log4j: Using URL [ile:/C:/x/y/myProject/target/test-classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/x/y/myProject/target/test-classes/log4j.properties
And I'm using the following log code:
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
LOG.warn("Very informative message");
My log4j.properties file:
# Define the root logger to the system property "hbase.root.logger".
log4j.rootLogger=DEBUG, console
# Logging Threshold
log4j.threshhold=ALL
#
# console appender
# Add "console" to rootLogger above if you want to use this
#
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %-5p [%t] %C{2}(%L): %m%n
Adding a specific line for my logger doesn't help as well
log4j.logger.com.my.path=DEBUG, console
And I can see that it is being recognized:
Parsing for [com.my.path] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category com.my.path set to DEBUG
But still nothing is being logged.
Replace this line:
log4j.rootLogger=DEBUG, console
with this:
log4j.rootLogger = ALL, Console
Delete these two lines:
log4j.threshhold=ALL
log4j.appender.console.target=System.out
Not sure what all of that pattern is doing, so I would replace this line
log4j.appender.console.layout.ConversionPattern=%d %-5p [%t] %C{2}(%L): %m%n
with this:
log4j.appender.Console.layout.conversionPattern=%m%n
Then do this:
final static Logger logger = Logger.getLogger(MyClassName.class);
logger.info("This is info");
Related
I am trying to write to my webapp logs to a .log file in the /logs directory in tomcat but the file is not generated nor is any logging output going to the console other than the spring logs and tomcat logs. When I run spring boot as a jar file with the embedded tomcat it writes to the log file just fine, but as soon as I deploy to tomcat via the webapps folder the application logs are no where to be found.
SpringBoot 2.1.2
Java 1.8
Tomcat 8.5
I have tried:
configuring LOGGING_CONFIG in setenv.sh
Multiple loggers.. logback, java utils etc..
application.properties:
logging.file=../logs/my-app.log
logging.level.org.springframework=INFO
logging.level.com.bose=DEBUG
log4j.properties for log4j:
log4j.rootLogger=${marge.log.level}, stdout, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=marge.log
#when stdout is also specified it will not write to the file
log4j.appender.file.MaxFileSize=1MB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# stdout uses PatternLayout
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] [%-5p] %n%m%n
# Print only messages of level DEBUG or above in the package com.bose
log4j.logger.com.app=${log.level}
Expected: when I deploy my webapp in /webapps the application logs (generated by log4j) should be in my-app.log in the /logs directory
Actual: No file is generated and no logs are even in stdout/console
By default spring boot uses logback as a logging binder, so the key concept here to exclude logback first then include log4j
Example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
After that add log4j 2 dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Add your log4j2.properties file to src/main/resources to be on classpath
Finally pay attention to what logging interface you are using this is important, with the above configuration you should use apache logging like:
package com.example;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger LOGGER = LogManager.getLogger(Application.class);
public static void main(String[] args){
ApplicationContext ctx = SpringApplication.run(Application.class, args);
LOGGER.info("Info level log message");
LOGGER.debug("Debug level log message");
LOGGER.error("Error level log message");
}
}
I'm using an external JAR library that creates a log file every time is used:
private void initLogger() {
try {
boolean var1 = true;
FileHandler var2 = new FileHandler("lib.log", 4096000, 1, var1);
var2.setFormatter(new SimpleFormatter());
this.logger = Logger.getLogger("POS");
this.logger.addHandler(var2);
} catch (IOException var3) {
var3.printStackTrace();
}
}
This, create the files lib.log , lib.log.lck, etc...
The problem is that my App is using log4j(from maven xml):
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
What I want is simple, I want to redirect all the calls of that lib log to my logger.
I've tried using jul-to-slf4j :
https://www.slf4j.org/legacy.html
<!-- https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
With this configuration (console + daily file):
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1, RollingAppender
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%-5p] %d %c - %m%n
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=pos.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n
And then put this code at the beginning:
LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
Logger.getLogger("POS").setLevel(FINEST);
With this, the file is created (lib.log) but it's empty. And the output doesn't go to pos.log ( the one configured in log4j).
What I'm missing? Thanks!
You did not include slf4j-log4j12 in your project (more).
Maybe Log4j JDK Logging Adapter is better solution.
I'm trying to use Apache log4j in Tomcat 7. I need the logs in DEBUG mode so I have used
log4j.rootLogger=DEBUG, stdout, file
I have also used file rolling so as to get the logs daily in a separate file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
but in the DEBUG mode all the debug related logs are logging into "catalina.out" file instead of the daily created log, I cannot see any DEBUG related logs in the daily created log file only INFO and SEVERE are present in it. And my another question is how to not to log everything into catalina.log file as the file size is keep on increasing
log4j.rootLogger=DEBUG, stdout, file
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.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Dependencies that I have used:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
If you are working in windows environment please add below line code to your File logging configuration:
log4j.appender.file.File=${catalina.home}\\yourLog.log
This will direct to tomcat->bin folder. If you want specifically in tomcat->logs folder please configure accordingly by going one step backwards.
It is because The Apache Tomcat has a custom version of Log4J (referenced as JULI) built in it.
From the Tomcat Documentation:
The internal logging for Apache Tomcat uses JULI, a packaged renamed
fork of Apache Commons Logging that, by default, is hard-coded to use
the java.util.logging framework. This ensures that Tomcat's internal
logging and any web application logging will remain independent, even
if a web application uses Apache Commons Logging.
You could use:
log4j.rootLogger=TRACE, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n %c{1}:%L - %m%n
I downloaded kafka-clients-0.9.0.0.jar with maven and i expect i would see logging like those in this link Kafka Logging
However i have no idea why i am not getting any logging, even I set the bootstrap.servers wrongly on purpuse, but it just got stuck without throwing any warning.
I added a few lines of code to print to a file using log4j and it seems work but no idea why Kafka cannot log event to log4j.
import org.apache.log4j.Logger;
public class ConsumerLoop implements Runnable {
final static Logger logger = Logger.getLogger(ConsumerLoop.class);
#Override
public void run() {
logger.warn("running!!!!!");
}
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2016-04-04 12:54:01 WARN ConsumerLoop:40 - running!!!!!
Note, there is slf4j-api-1.7.6.jar that came as a dependency of kafka. Even I included the required library slf4j-api-x.x.x.jar, slf4j-log4jx-x.x.x.jar and used slf4j to log even but still cannot get the kafka logs.
This is an old question but someone may still benefit.
Just put log4j.properties under /src/main/resources with data
log4j.rootLogger=TRACE, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %p %m (%c)%n
#log4j.appender.fileAppender=org.apache.log4j.FileAppender
#log4j.appender.fileAppender.File=kafka-request.log
#log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
#log4j.appender.fileAppender.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n
# Turn on all our debugging info
log4j.logger.kafka=TRACE,stdout
#log4j.logger.kafka.producer.async.DefaultEventHandler=DEBUG,stdout
#log4j.logger.kafka.consumer.PartitionTopicInfo=TRACE,stdout
#log4j.logger.kafka.request.logger=TRACE,fileAppender
#log4j.additivity.kafka.request.logger=false
#log4j.logger.kafka.network.Processor=TRACE,fileAppender
#log4j.additivity.kafka.network.Processor=false
#log4j.logger.org.I0Itec.zkclient.ZkClient=DEBUG
And add dependency
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
Then you can see logs in console.
my code is working fine if i right click on server and run as run on server , but when i try to deploy it on tomcat and try to run it it is giving me below error
log4j.properties file:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,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
error:
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:ERROR Could not find value for key log4j.appender.error
log4j:ERROR Could not instantiate appender named "error".
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
pom.xml :
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
The problem is in these two lines:
log4j.logger.Controller = INFO,error,stdout
log4j.logger.Client = INFO,error,stdout
You can only specify one logger level as the first argument (in this case INFO), and then the next two arguments are going to be considered the appender names to use for that logger (in this case error and stdout). Because no appender named error exists, you are getting the errors you've reported.
Check this out (https://logging.apache.org/log4j/1.2/manual.html) that shows an example of assigning a logger to two different appenders that looks a lot like your code. Hopefully this will help explain why log4j is looking for an appender named error in your application.
Here is another configuration file that uses multiple appenders.
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n