It would be nice to have an option to replace characters within the logging message with another character.
A good use case for this would be to replace newlines with some other character, so that every line of the log file contains one logging message. This is of particular importance when using a RollingFileAppender with multiple processes on the same file. Then log lines can get intermixed, which makes the log practically unusable.
I am not sure that I understand the problem you are describing. The pattern layout has the
%n
format specifier which inserts the system specific EOL.Ok, more specifically. I have two processes each logging to the same file. The first process will log "1a\nb", while the second process will log
2a\nb
. Now my log file sometimes looks like:1a
2a
b
b
Now if have no idea who logged b.
It would be nice if I could make a logger that replaces newlines by something else, say the pipe character. Then my log would look like
1a|b
2a|b
Alternatively, I could replace the newline with something else like prefix the process name. Then the output would become
Process1: 1a
Process2: 2a
Process1: b
Process2: b
This would also be great.
I hope you get the idea. If not, let me know.
When
UseLockFile
property is set onFileAppender
or its derived appenders, log4cplus will use OS specific file locking mechanism to synchronize and prevent the situation you mention above.Ok sorry I confused you with the processes. In fact, we run separate processes on different computers and aggregate the logs afterwards (which is where multi-line log messages get scrambled). A lock file won't help here.
So the feature request is:
Can you replace newline characters in a log string by a user defined prefix (ideally containing configurable stuff like hostname, date, pid, ...).
The attached patch does what I wanted. Instead of
%m
you can specify%~[AB]
which means: insert the log message here, with everyA
replaced byB
. Four special characters can be entered using a backslash as escape character:\n
(newline),\t
(tab),\r
(carriage return) and\\
(backslash).An even fancier version would really have regexpes (hence the
~
), but this satisfies my requirements and does not introduce dependencies. Also, my code may honour the formatting rules (not sure).Having something like this in the trunk would be great.