where I should place log4j.properties file to enable debug logging in smslib,when it is added through maven dependencies?

StackOverflow https://stackoverflow.com/questions/17264378

Question

I'm doing some java maven project based on thucydides-jbehave-archetype.

Smslib dependency is added through maven:

<dependency>
   <groupId>org.smslib</groupId>
   <artifactId>smslib</artifactId>   
   <version>dev-SNAPSHOT</version>
</dependency>
...
<repositories>
   <repository>
      <id>smslib-snapshots</id>
      <name>SMSLib Repository</name>
      <url>http://smslib.org/maven2/snapshots/</url>
   </repository>
</repositories>

Among the pure web tests I plan to do some sms sending/receiving; sms code is placed in src/main/java/projectname/gsm package (for instance, page objects are placed in src/main/java/projectname/pages, steps are placed in src/test/java/projectname.jbehave/). I'd like to enable debug messages for smslib, but not for thucydides. However, there was no location where log4j.properties worked for smslib, and there was one location, that resulted in appearing debug messages for thucydides.

Was it helpful?

Solution

You can dump your log4j.properties file in src/main/resources.

To control the log level, just create an appropriate Log4j configuration file.

...
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
...

For more details, please have a look at the Log4j manual.

OTHER TIPS

Can your question be rephrased as:

How do I set two different log levels using properties files for log4j - using two separate files for two classes in different packages?

If this is the case, rimero's answer is a start. But instead of dropping the properties files in src/main/sources, you need to drop each file (defining a different log level) in a directory structure reflecting the package name (for example src/main/projectname/page). Then, you can load each properties file separately using

PropertyConfigurator.configure(URL configURL)

There are a couple of ways to get the url of your properties files, see for example:

How to configure log4j with a properties file

Here is a full example:


public class TestPropertiesFile {

private static final String PREFIX = "projectname/pages";
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
        .getLogger("projectname.page.SomeClass");
private static String PROPERTIES_PATH;

static {
    PROPERTIES_PATH = Thread.currentThread().getContextClassLoader()
            .getResource(PREFIX + "/log4j.properties").getPath();
    org.apache.log4j.PropertyConfigurator.configure(PROPERTIES_PATH);

}

public static void test() {
    LOGGER.info("Logging from: " + PROPERTIES_PATH);
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top