0

I'm building a web service using jax-rs and wanted to log some stuff when urls are called. I found about log4j 2 and decided to use it. I dont know why but it works when I do some tests

Response response = target("user/v1/"+alfred.getPseudo()).request().get();
assertTrue(response.getStatus() < 300);

This will put the log String in logs/logs.log (I love logs). Yet, when I deploy (with glassfish 4.1.1), it doesn't write a single char in the file when I call the same URL.

Here's my log4j2.xml (located in src/main/resources) :

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
       <File name="File" fileName="logs/logs.log">
           <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}[%t] %c{1} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="File"/>
        </Root>
   </Loggers>
</Configuration>

My web.xml is empty :

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
</web-app>

I have the lo4j-api and log4j-core, both 2.5, in my pom.xml.

Finally, here's the code :

private final static Logger logger = LogManager.getLogger(MyClass.class);

on which I just call logger.info(String) and logger.error(String).

What am I doing wrong, and what should I do to create logs when deploying ?

Thanks.

EDIT : Perhaps this will help. I previously had

<File name="File" fileName="logs/logs.log" bufferedIO="false">

When I was testing, I had a warning "Hey, you set bufferedIO to false but the default buffer size is 8192, wtf". And this warning was being shown when i was calling the urls during real deployment. I guess that means my configuration was detected when deploying aswell.

Ghalnas
  • 194
  • 1
  • 8

2 Answers2

0

You need to tell the location in web.xml, I suppose.

<context-param> <param-name>log4jConfiguration</param-name> <param-value>WEB-INF/classes/log4j2.xml</param-value> </context-param>

Please check the deployment for the location of log4j2.xml and change the param-value accordingly.

But it is recommended to use a log4j2.xml at WAR level. So it is easy to find and change.

chaitan64arun
  • 783
  • 8
  • 20
  • Thanks for answering. Unfortunatly, putting the xml file in `WEB-INF/classes` and adding the context-param you gave in `web.xml` didn't do it. It no longer works with test (I guess that might be normal), and still don't add any line in `logs.log` – Ghalnas May 29 '16 at 08:40
0

Okay, so I understood my mistake from this : https://stackoverflow.com/a/8486370/5670024

I was trying to write the file inside the application's directory. Changed

<File name="File" fileName="logs/logs.log">
    <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}[%t] %c{1} - %msg%n"/>
</File>

to

<File name="File" fileName="c:/logs.log">
     <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS}[%t] %c{1} - %msg%n"/>
</File>

and it works !

Community
  • 1
  • 1
Ghalnas
  • 194
  • 1
  • 8