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

LOGMGR-182 support for logging complete MDC map #152

Merged
merged 1 commit into from
Jul 3, 2018

Conversation

jpraet
Copy link
Contributor

@jpraet jpraet commented Nov 10, 2017

When using %X in the log pattern without any key arguments, the complete
MDC map is now logged in format {key1=value1, key2=value2}.

When using %X in the log pattern without any key arguments, the complete
MDC map is now logged in format {key1=value1, key2=value2}.
@jpraet
Copy link
Contributor Author

jpraet commented Nov 25, 2017

Or would it be better to use JSON format?

{"key1":"value1", "key2":"value2"}

There is no real established convention at the moment:

  • logback: key1=val1, key2=val2
  • log4j 1.2: {{key1,val1},{key2,val2}}
  • log4j 2: {key1=value1, key2=value2}

@jpraet
Copy link
Contributor Author

jpraet commented Feb 7, 2018

Any chance of this getting merged? I'm using a custom PatternFormatter at the moment to work around this:

public class MdcExpandingPatternFormatter extends PatternFormatter {

	private static final Pattern EXPAND_MDC = Pattern.compile("%X(?!\\{)");

	private static final String MDC_KEY = "MDC";

	private boolean expandMdc = false;

	@Override
	public void setPattern(String pattern) {
		Matcher matcher = EXPAND_MDC.matcher(pattern);
		if (matcher.find()) {
			super.setPattern(matcher.replaceFirst("%X{" + MDC_KEY + "}"));
			this.expandMdc = true;
		} else {
			super.setPattern(pattern);
		}
	}

	@Override
	public String format(ExtLogRecord record) {
		if (expandMdc) {
			Map<String, String> mdc = record.getMdcCopy();
			mdc.remove(MDC_KEY);
			record.putMdc(MDC_KEY, mdc.toString());
		}
		return super.format(record);
	}

}

@fs-aikito
Copy link

I'd be interested in this, and could use the workaround too. How do I use that custom formatter? The documentation on formatters seems bit sparse... I'm using wildfly-swarm if that matters. I tried just copy-pasting the code in my project, but it didn't get picked up automatically, do I need to register it somewhere somehow?

@jpraet
Copy link
Contributor Author

jpraet commented Feb 22, 2018

We configured it like this in the jboss standalone.xml:

<formatter name="PATTERN">
    <custom-formatter module="be.fgov.kszbcss.tracer" class="be.fgov.kszbcss.log.formatter.MdcExpandingPatternFormatter">
        <properties>
            <property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %X %s%e%n"/>
        </properties>
    </custom-formatter>
</formatter>

The custom formatter class was put in a shared global module. Don't know if/how that would work in wildfly-swarm.

@jamezp
Copy link
Member

jamezp commented Jul 3, 2018

This should make it into WildFly 14.

@jamezp jamezp merged commit 2cc3e43 into jboss-logging:master Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants