1

I have a java application. I am using a log formater object. All log messages should have this object. for example

log.debug(new LogFormatter(x,y,z)) 

But I have to create new LogFormatter object each and everything i want to log. If I use static method for example

log.debug(LogFormatter.format(x,y,z)) 

Than I don't have to create new objects. But in a multithreaded application would it work right.

if two threads call with diff values, woud the logging get messed up.

Or is thread local the best way to go

2
  • This depends on the code inside the format method. Could you post the method implementation? Commented Oct 12, 2012 at 14:39
  • I think log will print timestamp, thread-id/name with it and I think you won't have issue with LogFormatter.format(..) Commented Oct 12, 2012 at 14:43

4 Answers 4

1

I would use

private final LogFormatter logFormatter; // immutable object. log.debug(logFormatter.format(x,y,z)) 

as the formatter is immutable it can be shared.

Sign up to request clarification or add additional context in comments.

4 Comments

Inapplicable for static methods
The first example given suggests the method don't have to be static. I suggest avoiding static methods for this sort of thing if you can as you may want to be able to change the formatter.
But if the LogFormatter.format(...) method is stateless (it doesn't use any LogFormatter instance global fields) doesn't that alone suffice to ensure thread safety (no matter if it's static or not (obviously it'd have to be final if non-static so that no stateful overrides can be created in a class that would extend LogFormatter))?
If the object is stateless, you may still want to be able to change the formatter class to give a different format.
1

This would depend on the logger implementation. If you will use native Java classes, then you should handle the locks by using the synchronize keyword for the method or the code section that handles the log insertion.

IMO I would recommend to use a logging library like Log4J that is thread safe:

Note that some Java Application Servers like JBoss and GlassFish use Log4j to handle the logging work.

3 Comments

Used synchronized logs is inapplicable to highly concurrent application: it's a bottleneck.
@Aubin synchronized is trivial compared with the cost of writing a log entry. I suggest you don't write so many logs this way that it would ever matter.
The behavior of a multi treaded highly concurrent application with logging on is completely different when you switch the logging off above all a bottleneck.
0

That depends on two things:

  1. Whether the LogFormatter.format(x,y,z) method is thread safe.
  2. Whether your logger implementation is thread safe.

If these two conditions are true, then you can use your scheme in a multithreaded environment and expect no errors.

Specifically, your logger implementation should be thread-safe in the sense that it synchronizes access to the underlying output mechanism: for example, by ensuring that only one log record is printed at a time.

Comments

0

To avoid synchronization between thread only when logging is on, I'm used to associate one log chain per thread. The difficulty is to join several files to - off line - understand what's happening. Each line of log has a date, and a thread id.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.