I'm trying to use logback as my logger in my simple program but it does not work fine!
I put logback/logback.xml and logback/Logback.java in the source directory logback and run through this command line
\logback>java -cp .;%CLASSPATH% Logback
which the %CLASSPATH% is an environment variable that has the path of .jar file that logback needs like:
logback-access-1.1.2.jar
logback-classic-1.1.2.jar
logback-core-1.1.2.jar
slf4j-api-1.7.6.jar
This is my logback.xml file
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log</fileNamePattern>
<minIndex>1</minIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>2MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
and there is my simple program
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Logback{
private final static Logger logger = LoggerFactory.getLogger(Logback.class);
public static void main(String[] args){
for(int i=0;i<1000000;i++)
logger.debug("hello");
}
}
but unfortunately i just receive log in the console instead of test.log files. it seems the logger object just use the default configuration!!!
If i set the -Dlogback.configurationFile=logback.xml variable as below, it works properly. but how to run without this variable?
\logback>java -cp .;%CLASSPATH% -Dlogback.configurationFile=logback.xml Logback
From logback documentation:
1. Logback tries to find a file called logback.groovy in the classpath.
2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
3. If no such file is found, it checks for the file logback.xml in the classpath..
4. If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
Where should the configuration files such as logback.groovy, logback-test.xml or logback.xml be located on the classpath?
Configuration files such as logback.groovy, logback-test.xml or logback.xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.jar;c:/mylibs/" then the logback.xml file should be located directly under "c:/mylibs/", that is as "c:/mylibs/logback.xml". Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.
For web-applications, configuration files can be placed directly under WEB-INF/classes/.
So you need to put logback.xml in the classpath. On one project we had a similar problem although logback.xml was in the right place. Renaming it to logback-test.xml helped.
If you add the jar files to C:\Program Files\Java\jre7\lib\ext . and try to run the program like below:
\logback>java Logback
it wont see the logback.xml file in the source directory
when i deleted the
logback-access-1.1.2.jar
logback-classic-1.1.2.jar
logback-core-1.1.2.jar
slf4j-api-1.7.6.jar
files from C:\Program Files\Java\jre7\lib\ext, it went OK!
Related
I'm running Wiremock as a Standalone process (v2.5.1). I've created a Java custom transformer by extending: com.github.tomakehurst.wiremock.extension.ResponseTransformer
My custom transformer then uses some other common code that uses Log4J for logging. With code like:
import org.apache.log4j.Logger;
private static Logger logger = Logger.getLogger(CommonCode.class);
...
logger.error("This is some error");
Is there anyway I can configure Wiremock to output this custom logging? I've tried putting a log4j.xml and log4j.properties file in the classpath. Here's an example of a properties file:
log4j.appender.CUSTOMAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.CUSTOMAPPENDER.File=c:/WireMock/logs/custom.log
log4j.appender.CUSTOMAPPENDER.layout=org.apache.log4j.PatternLayout
log4j.logger.com.myorg=DEBUG, CUSTOMAPPENDER
The equivalent log4j.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CUSTOMAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/WireMock/logs/custom.log"/>
<param name="datePattern" value="'.'yyyy-MM-dd"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss}] [%r] %5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<logger name="com.myorg" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="CUSTOMAPPENDER"/>
</logger>
</log4j:configuration>
I also have the following JARs in my classpath:
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
I'd like this custom logging to go to a separate log file from the Default Wiremock verbose logging. Any help would be appreciated.
Once I realised it was simply that log4j could not find the log4j.properties or log4j.xml file I was able to resolve the issue. Basically the log4j properties file must be in the classpath for the Wiremock Java process.
I was running the Wiremock Server via a batch script which looked like this:
"C:/Java/jdk1.8.0_102/bin/java" -Dfile.encoding=UTF-8 -cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port="9091" --extensions "com.myorg.CustomTransformer" --root-dir="C:/WireMock" --verbose > "C:/WireMock/logs/wiremock.log"
My log4j.xml file was in my C:/WireMock/lib directory. I assumed that because I had a classpath entry of cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" which included C:/WireMock/lib/* that it would pick up my log4j properties. I also explicitly added the properties file to this classpath. Neither of these worked (hence this question). My Custom transformer class com.myorg.CustomTransformer is present in a jar also in the lib directory (which is picked up).
However after some research I found 2 ways to get the log4j properties file to be picked up.
Specify the log4j properties location as another -D parameter in the batch script. If I added -Dlog4j.configuration=file://c:/WireMock/lib/log4j.xml
Update my custom jar build script to include the log4j.xml file itself in the jar.
I have a Java standalone app. with a main method, with these 2 imports:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and
private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class);
In the same folder I also have a logback.xml but I don't know how to tell the program that uses the logback.xml to config the log
I couldn't find any class similar to org.apache.log4j.PropertyConfigurator
I have this logback.xml in the same folder of the Java class I am running:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>pecador.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>handler.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.springframework.orm.jpa" level="debug" /> -->
<logger name="com.calzada.pecador" level="debug" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
I also run the appl. with Program arguments:
-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml
and I got this error:
java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES
The library used is logback-classic-1.2.3.jar
Here's how logback configures itself:
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath.
If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
You can also point Logback at a specific configuration file using the system parameter logback.configurationFile. From the docs:
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
All the above taken from the docs.
According to your question you have a logback.xml but this file is in the "same folder" as your class. Unless your class is in the root package this means that logback.xml is not in the root of the classpath so Logback will not discover it. In order for Logback to configure itself from this file you can do one of the following:
Place your logback.xml in the root of your classpath (for a Maven project this is as simple as copying logback.xml to src/main/resources)
Run your Java program with -Dlogback.configurationFile=/path/to/logback.xml
I'm completely new to LogBack, and I'd like to use it. I notice that you can use a Configuration as an XML, but I have no idea how to implement that XML and have Logback use that instead of the default one.
My programs packages are like me.iarekylew00t.a.b and so on. It'll also be compiled into a runnable JAR at the end. How should I go about adding a logback.xml file, without the enduser needing to have it?
Sorry if this is a noob question, but I've been trying to look this up for hours and I can't find anything that clearly tells someone how to go about adding a configuration - most assume you already know how... Thanks. (please be as detailed as possible)
Add a logback.xml in the root of your classpath containing (tune to your needs):
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</encoder>
</appender>
<logger name="some.logger.name" level="INFO"/>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
and that's all.
EDIT: usual directory structure:
src/main/java/
some/package/
someClass.java
some/other/package/
someOtherClass.java
src/main/resources/
logback.xml
in the jar:
some/package/
someClass.class
some/other/package/
someOtherClass.class
logback.xml
I had the same issue and resolved it by copying logback.xml to src/main/java - this was the only way I found to make the executable JAR find it and use it.
Since I didn't want to have two (possibly different) config files, I kept the original logback.xml in my src/resources folder (where Eclipse would find it and use it during development) and replaced the one under src/main/java with a symlink. Works well for me.
I know there is allot of questions been asked on this but i have been trying to get this to work for few day's and i am not any more forward then when i started.
i have tried to use -Dlog4j.configuration=file:/path/to/log4j.properties and -Dlog4j.debug in eclipse vm arguments (under debug & run) and get no output
I have tried to use .properties and .xml but no joy
Tried to put the .xml and .properties files at the root, in the src and in an external folder which i added to my classpath ... still no joy
I think its using another .xml or .properties files in another lib/jar but because i cant get any debug to work i am finding very difficult to track what i am doing wrong here...
any help would be great! below is the code .. only the error message get's printed.
I have download (http://logging.apache.org/log4j/2.x/download.html) and imported into my app the
log4j-api-2.0-beta8.jar
log4j-core-2.0-beta8
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class CucmServMonitor
{
private static final Logger logger = LogManager.getLogger(CucmServMonitor.class.getName())
public static void main(String[] args)
{
logger.error("testing ERROR level");
logger.trace("exiting application");
System.out.println(logger.getName());
}
}
the xml file i am using just now log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
Manage to figure this one out. The hint was here.
http://pic.dhe.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-112.htm
I needed to add a "class folder" of where the log4j2.xml was located and then make sure it was at TOP of the list:
Right click on your project and go to properties
Then follow the step shown below. After adding the folder make sure its at the top and then click ok
Or... just create a resources directory like src/test/resources and add the log4j.xml file to that dir and then make that directory a source folder. Then eclipse will automatically copy the file to the the classes dir and there you have it.
1) Create the log4j.properties file inside the root folder
log4j.rootCategory=DEBUG, CONSOLE
# Appender writes to console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
2) Modify the code like this
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class CucmServMonitor {
private static final Logger logger = Logger.getLogger(CucmServMonitor.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.error("testing ERROR level");
logger.trace("exiting application");
System.out.println(logger.getName());
}
}
according to Alexis comment, I setup it, but eclipse still can not find log4j2.xml. finally, I solved it by removing the other jars imported, only keep log4j-api and log4j-core. before I imported all the jars downloaded from the Apache website. hope this can help someone.
According to Alexis comment, I setup it, but eclipse still can not find log4j2.xml. finally, I solved it by removing the other jars imported, only keep log4j-api and log4j-core. before I imported all the jars downloaded from the Apache website.
I solved my project for log4j->log4j2 version upgrading.
I'm working on a project where I thought I would try using logback-classic for debugging and log rotation. I'm using this in a Maven context for building and creating a .war file to be deployed in JBoss 7.1 Application Server.
I've placed a logback.xml file in the resources folder in the code and a logback-test.xml in test/resources.
The active jar I'm using is the SLF4J to print the actual debugging.
public class MyClass extends MultiActionController {
private Logger logger = LoggerFactory.getLogger(MyClass.class);
public MyClass() {
logger.debug("hello");
}
}
When I run a JUnit test on the code in Maven itself it works, but after building a .war file i don't get any debugging in STDOUT nor can I find a file created.
I know that I've removed the actual logging from STDOUT in the config file, but where is the logging going...
the logback.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/myproject.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyMMdd HH:mm:ss.SSS} [%-5.5level] [%-25.25logger] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
The folder "logs" do I need to explicit create it in the JBoss instance or need to refer relative to it for this to work? Or is there something I missed? Do I need to put the logback.xml file in the JBoss instance?
best,
Henrik
Firstly you should ensure that the logback.xml is at the classpath, e.g. WEB-INF/classes/logback.xml as the mentioning at Chapter 3: Logback configuration.
The Chapter 4: Appenders mentions about FileAppender and RollingFileAppender properties as
file *String* The name of the file to write to. If the file does not exist, it is created. On the MS Windows platform users frequently forget to escape back slashes. For example, the value c:\temp\test.log is not likely to be interpreted properly as '\t' is an escape sequence interpreted as a single tab character (\u0009). Correct values can be specified as c:/temp/test.log or alternatively as c:\temp\test.log. The File option has no default value.
If the parent directory of the file does not exist, FileAppender will automatically create it, including any necessary but nonexistent parent directories.
The example is for logback.xml is as the following:-
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
I hope this may help.
You need to define the file-tag (as the previous answers already said). But you also need to know how it's interpreted. Logback sees it as relative to the application it's used in. So if you only use
<file>logs/<myfile>.log</file>
logback should create a "logs"-folder in the root of your JBoss (more specific: the folder where the .sh-script you start it with is located). Check from the root of your JBoss to see if you can find your logs-folder and file from there.