Currently I have sth like:
%d{HH:mm:ss.SSS} | %-5.5p | %-15.15t | %-30.70C.%-5.20M:%-3.5L | %msg%n
but this makes full.class.Name.method:line of different lengths - is it possible to make calss.method:line of fixed length ?
I would like sth like
%-60.60CML (but this of course doesn't work)
I would like to make calss.method.line have the same length on all log lines. (so as the actual message started in the same place)
Is it possible ?
Other wors said, is it possible to format multiple conversion characters with one conversion specifier ? Looking at
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
Each conversion specifier starts with a percent sign (%) and is followed by optional format modifiers and a conversion character. The conversion character specifies the type of data
The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character.
It's now written about multiple conversion characters, so maybe it's simply not possible ?
As @Remko Popma explained, what I want is not possible now:
No, each modifier applies to one conversion. But Log4j could add a new converter that means what you want. You can propose such a new converter on the mailing list or issue tracker.
I created issue: https://issues.apache.org/jira/browse/LOG4J2-2339
Based on help from @Remko-Popma
https://issues.apache.org/jira/browse/LOG4J2-2339
below is what worked for me:
<property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5.5p | %-15.15t | %-70.70CML | %-10.70X | %msg%n</property>
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.NamePatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
@Plugin(name = "ClassMethodLinePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({"CML", "classmethodline"})
public class ClassMethodLinePatternConverter extends NamePatternConverter {
private static final String NA = "?";
private ClassMethodLinePatternConverter(
final String[] options) {
super("Class Name", "class name", options);
}
public static ClassMethodLinePatternConverter newInstance(final String[] options) {
return new ClassMethodLinePatternConverter(options);
}
/**
* Format a logging event.
*
* @param event event to format.
* @param toAppendTo string buffer to which class name, method name and line will be appended.
*/
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
final StackTraceElement element = event.getSource();
if (element == null) {
toAppendTo.append(NA);
} else {
toAppendTo.append(element.getClassName()).append(".").append(element.getMethodName()).append(":").append(element.getLineNumber());
}
}
}
Sorry I didn't find out that Log4j is 1.x in the question, this answer is for Log4j 2.x. The answer below is reserved for those who may need it:
You can use style{pattern}{ANSI style} conversion pattern, just change %-30.70C.%-5.20M:%-3.5L to %-60style{%-30.70C.%-5.20M:%-3.5L}.
Simply explain, just use %-60style{replace-your-pattern-here} to wrap the pattern part you want to format, and adjust the width value according to your situation (here is 60 characters width). If you want colored output, just add the omitted {ANSI style} after style{pattern}.
Here is Log4j2 doc: https://logging.apache.org/log4j/2.x/manual/layouts.html#Patterns
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With