• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Log4j will not write to log file.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really sure how to do that.

Is that just a matter of putting C:\error.log as the logpath?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ran as is and log file is still currently empty.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
al yosha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think if above code may help you !
 
shalindra Singh Suryvanshee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic