0

I am using Tomcat as a server. I amb also using

This is my pom file:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.21</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.7</version>
        </dependency>

And this is my log4j.properties placed on src/main/sources

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
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
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

And this is how I am using it on my classes:

 private static final Logger logger = LoggerFactory.getLogger(getClass());

 //...

 logger.debug("TEST");

I am able to see the logger messages on the console output when I run it on Netbeans. I know it is recorded on catalina.out but I am not getting example.log anywhere. Where it is supposed to be created?

I am using tomcat embedded on Netbeans, so the path to it is C:\Users\<user>\AppData\Roaming\NetBeans\8.1\apache-tomcat-8.0.27.0_base but I can't find example.log anywhere. I don't want to check catalina.out so this is why I want my own log messages to be stored in example.log.

What am I doing wrong?

Thank you!

2 Answers 2

0

It looks like you're using Logback in your dependencies, but trying to configure log4j.

If you want to use log4j as your logging backend for SLF4J, then use the slf4j-log4j12 and log4j dependencies instead of logback in your pom.xml.

If you want to use logback as your logging backend, then use logback's configuration files rather than one for log4j.

1
  • changed file nome to logback.xml and now it is working ( console output has changed so I know I amb using my .xml file for formatting the log output ). However, I can't find the .log file. Where it is supposed to be stored? Thanks!
    – Toni
    May 31, 2016 at 14:59
0

Solved, now it is working fine!

<configuration>

    <property name="DEV_HOME" value="c:/logs" />

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${DEV_HOME}/safeMO.log</file>
        <encoder>
            <pattern>[%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.