File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Java Products and Servers and the fly likes Log4j will not write to log file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "Log4j will not write to log file." Watch "Log4j will not write to log file." New topic
Author

Log4j will not write to log file.

al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12


file is in the correct directory. web.xml file is correct and loads servlet on startup. But log is file is always blank.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't know why that doesn't work, but I do know that there are two things there which I wouldn't do.

(1) Configuring log4j in code rather than in an external configuration file.

(2) Writing log files into files within the web application's directories.

I suggest if you stop doing those two things your problem will go away.
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
I'm working through a tutorial from a book. Not really in a position to do it any other way at the moment as if i did it would probably make the rest of the book hard to make sense of regarding anything to do with log4j.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ah, okay. Just a tutorial? Then carry on with the rest of the tutorial... or contact the writers of the tutorial for help.
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
Well I'd need to get this working to carry on with the tutorial and I'd rather see if anyone could have a stab and understanding why it doesn't work before I try to contact the author.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Okay then. First step: change the code so that the log files are written outside the web app's directories. Outside the server's directories for that matter.
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
Not really sure how to do that.

Is that just a matter of putting C:\error.log as the logpath?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I wouldn't have chosen the root directory myself, but yeah, just choose somewhere reasonable (i.e. not within the web container) to put the logs.
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12


ran as is and log file is still currently empty.
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
you may write like below -
RollingFileAppender appender =
new RollingFileAppender(new PatternLayout("%-4r [%t] %-5p %c %x - %m%n")
, getServletContext().getRealPath("/logs/error.log"), true);

your logs directory would be parallel to WEB-INF.

Try out this.

Thanks
Shalindra Singh


Regards
Shalindra Singh
SCJP, SCWCD
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
shalindra Singh Suryvanshee wrote:you may write like below -
RollingFileAppender appender =
new RollingFileAppender(new PatternLayout("%-4r [%t] %-5p %c %x - %m%n")
, getServletContext().getRealPath("/logs/error.log"), true);

your logs directory would be parallel to WEB-INF.

Try out this.

Thanks
Shalindra Singh


Do you mean create the log directory one level up from where it is at the moment? Why would this make a difference?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

al yosha wrote:Do you mean create the log directory one level up from where it is at the moment? Why would this make a difference?


I don't think it would make a difference either.

But what have you established so far? You said that your log file was "empty". That implies to me that something created the log file, or you would have said it doesn't exist. Did you do that, or did log4j do it? (Or does it in fact not exist?)
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
There are two suggestion if above code does not work -
1. try to print the output of getContext().getRealPath(...)
2. try with appender.setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)

Thanks
Shalindra Singh
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
Paul Clapham wrote:
al yosha wrote:Do you mean create the log directory one level up from where it is at the moment? Why would this make a difference?


I don't think it would make a difference either.

But what have you established so far? You said that your log file was "empty". That implies to me that something created the log file, or you would have said it doesn't exist. Did you do that, or did log4j do it? (Or does it in fact not exist?)


No, the books says to make sure the file already exists before hand and to make sure it's in a a directory called log under WEB-INF. So i created a an "empty file" in netbeans, gave it a .log extension and placed it into a newly created directory called logs. Log4j is not overwriting this file or placing any info into it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

al yosha wrote:No, the books says to make sure the file already exists before hand...


Well, I'm sorry, but I'm giving up on this tutorial. First it recommends two separate bad practices in one example, and now it's telling you that you have to create log files in advance?

Seriously. It's rubbish. Forget about it.
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
The books Java Web Development using Hibernate, JSP and Servlets.

to quote.

One advantage of using a servlet to initialise Log4j is that the location of the actual
log file can be specified using a relative reference from the root of the current web
application. This allows the log file to be made portable; whenever the web application
is deployed, the log file will be deployed with it. In order to retrieve the path
to the root of the web application on the current computer, use the two methods
getServletContext().getRealPath().

The log file should never be placed in a location that can be viewed from the
web. By placing the log file in the WEB-INF directory, it cannot be accessed from
the web. Use the following command to specify that the log file is located in a file
named error.log located in the logs subdirectory of WEB-INF. Be sure that the file
already exists and is writable by the servlet engine.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It says that? That completely disregards the fact that if you redeploy the application, all of the log files you previously put there will be lost.
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25

I think if above code may help you !
shalindra Singh Suryvanshee
Greenhorn

Joined: Aug 05, 2008
Posts: 25
Ok ! I think -
you set level as DEBUG in your code and trying to print log of INFO level
logger.setLevel(Level.DEBUG);
//logger.addAppender(appender);
logger.info("Starting " + logger.getName());
al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
my current code.



logpath comes out as C:\Documents and Settings\user\My Documents\NetBeansProjects\WebApplication2\build\web\WEB-INF\logs\error.log which is the same address as the log file in the files view of netbeans.

still no content.


al yosha
Greenhorn

Joined: Mar 07, 2011
Posts: 12
I've solved this now. Everything was actually fine with the code. It was just that becase the file is generated at run time it only exists in under the /build directory and the changes are not reflected in the log file under the projects view. Also it seems to make no difference if the file exists to begin with or not.

Thanks for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4j will not write to log file.