| Author |
thread safe logging in a servlet
|
John Schretz
Ranch Hand
Joined: Sep 10, 2008
Posts: 171
|
|
using log4j would the following example be considered thread safe?
|
 |
John Schretz
Ranch Hand
Joined: Sep 10, 2008
Posts: 171
|
|
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
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Duc Vo
Ranch Hand
Joined: Nov 20, 2008
Posts: 254
|
|
(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
|
“Everything should be as simple as it is, but not simpler.” Albert Einstein
|
 |
John Schretz
Ranch Hand
Joined: Sep 10, 2008
Posts: 171
|
|
ok so for read write would the example below be correct?
|
 |
John Schretz
Ranch Hand
Joined: Sep 10, 2008
Posts: 171
|
|
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
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
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
Joined: Sep 10, 2008
Posts: 171
|
|
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
Joined: Nov 20, 2008
Posts: 254
|
|
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
Joined: Sep 10, 2008
Posts: 171
|
|
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
Joined: Nov 20, 2008
Posts: 254
|
|
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
Joined: Sep 10, 2008
Posts: 171
|
|
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
|
 |
 |
|
|
subject: thread safe logging in a servlet
|
|
|