Skip to content

Commit

Permalink
Add parser for %method (Issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tony19 committed Jul 22, 2013
1 parent a70d8c4 commit e697917
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/ch/qos/logback/decoder/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}};
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/ch/qos/logback/decoder/MethodOfCallerParser.java
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);
}

}
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();
}
}

0 comments on commit e697917

Please sign in to comment.