• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

thread safe logging in a servlet

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using log4j would the following example be considered thread safe?


 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and to further explain

would i declare String realPath = getServletContext().getRealPath("/");
String fileSep = System.getProperty("file.separator");

in the class declaration or the servlet doGet method?

i think im just confusing myself

i was under the impression that nothing should be declared global in a servlet


 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Declaring the logger at class level ("global" isn't the correct term) is fine as Log4J handles the thread safety issue internally.

In general, you want to keep read/write data local to the methods.

If data needs to be shared across threads, it can safely be done as long as the threads only read the data, or if they synchronize access to read/write data.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) Log4j is thread safe by design, so you don't have to worry about question 1.
(2) Log4j will search for the first log4j.properties file on the classpath to configure itself when loaded, normally it will be from WEB-INF/classes/log4j.properties. Hence, you don't have to do the code as in question 2.

Hope it help,

Duc
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so for read write would the example below be correct?

 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Duc Vo wrote:(1) Log4j is thread safe by design, so you don't have to worry about question 1.
(2) Log4j will search for the first log4j.properties file on the classpath to configure itself when loaded, normally it will be from WEB-INF/classes/log4j.properties. Hence, you don't have to do the code as in question 2.

Hope it help,

Duc




ok but if i want log4j to log to 2 seprate log files with different names can that be done by having 2 properties files and
adding:




thanks
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Schretz wrote:ok so for read write would the example below be correct?

Yes. The variables are local to the method and the thread.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

John Schretz wrote:ok so for read write would the example below be correct?

Yes. The variables are local to the method and the thread.




Thanks again bear
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, it is a wrong approach for the problem. If you want to send log entries to two different files, you'll only need two appenders declared in the same log4j.properties file.

I.e. say I want to log all entries to a debug.log file, and all error entries to a error.log file, also all log entries will be sent to the CONSOLE. I'll declare my log4j.properties as below.
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Duc Vo wrote:Unfortunately, it is a wrong approach for the problem. If you want to send log entries to two different files, you'll only need two appenders declared in the same log4j.properties file.

I.e. say I want to log all entries to a debug.log file, and all error entries to a error.log file, also all log entries will be sent to the CONSOLE. I'll declare my log4j.properties as below.



what if i wanted to specify a file eg.
'SysLog.txt'

all i want to use is logger.info()
but i want to be able to log to 2 custom files

'SysLog1.txt'
'SysLog2.txt'

below is my config file





 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,

The answer is actually in my previous post.

Just create A3 which writes to SysLog2.txt, sets the Threshold to INFO, then configure the logger to use A3 as well i.e.
log4j.rootLogger = , A1, A2, A3
 
John Schretz
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the late response. Thanks that makes sense, the first time i read it i maut have read it too fast and overlooked the actual answer lol
thanks again
john
 
reply
    Bookmark Topic Watch Topic
  • New Topic