Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add threadId to PatternLayout #711

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@
import ch.qos.logback.core.pattern.parser.Parser;

/**
* <p>
* A flexible layout configurable with pattern string. The goal of this class is
* to {@link #format format} a {@link ILoggingEvent} and return the results in a
* {#link String}. The format of the result depends on the <em>conversion
* to format a {@link ILoggingEvent} and return the results in a
* {@link String}. The format of the result depends on the <em>conversion
* pattern</em>.
* <p>
* For more information about this layout, please refer to the online manual at
* http://logback.qos.ch/manual/layouts.html#PatternLayout
*
* For more information about this layout, please refer to
* <a href="http://logback.qos.ch/manual/layouts.html#PatternLayout">the online manual</a>.
*/

public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {

public static final Map<String, String> DEFAULT_CONVERTER_MAP = new HashMap<String, String>();
Expand Down Expand Up @@ -73,6 +70,10 @@ public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {
DEFAULT_CONVERTER_MAP.put("thread", ThreadConverter.class.getName());
CONVERTER_CLASS_TO_KEY_MAP.put(ThreadConverter.class.getName(), "thread");

DEFAULT_CONVERTER_MAP.put("tid", ThreadIdConverter.class.getName());
DEFAULT_CONVERTER_MAP.put("threadId", ThreadIdConverter.class.getName());
CONVERTER_CLASS_TO_KEY_MAP.put(ThreadIdConverter.class.getName(), "threadId");

DEFAULT_CONVERTER_MAP.put("lo", LoggerConverter.class.getName());
DEFAULT_CONVERTER_MAP.put("logger", LoggerConverter.class.getName());
DEFAULT_CONVERTER_MAP.put("c", LoggerConverter.class.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
*
* This program and the accompanying materials are dual-licensed under
* either the terms of the Eclipse Public License v1.0 as published by
* the Eclipse Foundation
*
* or (per the licensee's choosing)
*
* under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation.
*/
package ch.qos.logback.classic.pattern;

import ch.qos.logback.classic.spi.ILoggingEvent;

/**
* Returns the id of the current thread.
*
* @author Roberto Cella
*/
public class ThreadIdConverter extends ClassicConverter {

public String convert(ILoggingEvent event) {
return String.valueOf(Thread.currentThread().getId());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be present in the ILoggingEvent and retrieved from there? Otherwise this can return the thread id of an internal logback thread, not the id of the actual caller thread, e.g. in an AsyncAppender. See prepareForDeferredProcessing() in the default impl class LoggingEvent.

}

}
1 change: 1 addition & 0 deletions logback-classic/src/test/input/fqcn.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ ch.qos.logback.classic.pattern.RootCauseFirstThrowableProxyConverter
ch.qos.logback.classic.pattern.SyslogStartConverter
ch.qos.logback.classic.pattern.TargetLengthBasedClassNameAbbreviator
ch.qos.logback.classic.pattern.ThreadConverter
ch.qos.logback.classic.pattern.ThreadIdConverter
ch.qos.logback.classic.pattern.ThrowableHandlingConverter
ch.qos.logback.classic.pattern.ThrowableProxyConverter
ch.qos.logback.classic.pattern.Util
Expand Down