diff --git a/src/main/java/ch/qos/logback/decoder/Decoder.java b/src/main/java/ch/qos/logback/decoder/Decoder.java index bb41245..f14d850 100644 --- a/src/main/java/ch/qos/logback/decoder/Decoder.java +++ b/src/main/java/ch/qos/logback/decoder/Decoder.java @@ -145,6 +145,7 @@ private PatternInfo getPatternInfo(int patternIndex, String fieldName) { put(PatternNames.LEVEL, new LevelParser()); put(PatternNames.LINE_OF_CALLER, new LineOfCallerParser()); put(PatternNames.LOGGER_NAME, new LoggerNameParser()); + put(PatternNames.METHOD_OF_CALLER, new MethodOfCallerParser()); put(PatternNames.MESSAGE, new MessageParser()); put(PatternNames.THREAD_NAME, new ThreadNameParser()); }}; diff --git a/src/main/java/ch/qos/logback/decoder/MethodOfCallerParser.java b/src/main/java/ch/qos/logback/decoder/MethodOfCallerParser.java new file mode 100644 index 0000000..cb1de39 --- /dev/null +++ b/src/main/java/ch/qos/logback/decoder/MethodOfCallerParser.java @@ -0,0 +1,28 @@ +/** + * Copyright (C) 2013, 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.decoder; + +import ch.qos.logback.core.pattern.parser2.PatternInfo; + +/** + * A {@code MethodOfCallerParser} parses a method-of-caller field (%method) from a string + * and populates the appropriate field in a given logging event + */ +public class MethodOfCallerParser implements FieldCapturer { + + @Override + public void captureField(IStaticLoggingEvent event, String fieldAsStr, PatternInfo info) { + event.setMethodOfCaller(fieldAsStr); + } + +} diff --git a/src/test/java/ch/qos/logback/decoder/MethodOfCallerDecoderTest.java b/src/test/java/ch/qos/logback/decoder/MethodOfCallerDecoderTest.java new file mode 100644 index 0000000..3fa39e0 --- /dev/null +++ b/src/test/java/ch/qos/logback/decoder/MethodOfCallerDecoderTest.java @@ -0,0 +1,81 @@ +/** + * Copyright (C) 2013, 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.decoder; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +/** + * Tests decoding %logger + * + * @author Anthony Trinh + */ +public class MethodOfCallerDecoderTest extends DecoderTest { + + @Test + public void decodesSimpleMethodName() { + assertEquals("fooBar123", getMethodName("fooBar123")); + } + + @Test + public void decodesMethodNameContainingUnderscore() { + assertEquals("foo_bar_123", getMethodName("foo_bar_123")); + } + + @Test + public void decodesMethodNameThatBeginsWithUnderscore() { + assertEquals("_fooBar123", getMethodName("_fooBar123")); + } + + // should never happen because identifiers cannot begin with a digit + // but Decoder should be able to parse it anyway + @Test + public void decodesMethodNameThatBeginsWithNumber() { + assertEquals("0fooBar123", getMethodName("0fooBar123")); + } + + @Test + public void noMatchWhenMethodNameHasSpaces() { + assertNoEventWhenMethodNameIs("fooBar123 Baz"); + } + + @Test + public void noMatchWhenMethodNameHasDots() { + assertNoEventWhenMethodNameIs("foo.Bar.123"); + } + + @Test + public void noMatchWhenMethodNameMissing() { + assertNoEventWhenMethodNameIs(" "); + } + + private void assertNoEventWhenMethodNameIs(String value) { + final String INPUT = "2013-06-12 15:27:15.044 INFO [" + value + "]: foo bar message\n"; + final String PATT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%method]: %msg%n"; + decoder.setLayoutPattern(PATT); + StaticLoggingEvent event = (StaticLoggingEvent)decoder.decode(INPUT); + assertNull(event); + } + + private String getMethodName(String methodName) { + final String INPUT = "2013-06-12 15:27:15.044 INFO [" + methodName + "]: foo bar message\n"; + final String PATT = "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%method]: %msg%n"; + decoder.setLayoutPattern(PATT); + StaticLoggingEvent event = (StaticLoggingEvent)decoder.decode(INPUT); + assertNotNull(event); + return event.getMethodOfCaller(); + } +}