-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/ch/qos/logback/decoder/MethodOfCallerParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IStaticLoggingEvent> { | ||
|
||
@Override | ||
public void captureField(IStaticLoggingEvent event, String fieldAsStr, PatternInfo info) { | ||
event.setMethodOfCaller(fieldAsStr); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/test/java/ch/qos/logback/decoder/MethodOfCallerDecoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |