-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): add encoder to avoid any external usage
- Loading branch information
1 parent
e693d8a
commit 6c51cff
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
common/src/main/java/org/tron/common/log/layout/MultiLayoutPattern.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,106 @@ | ||
package org.tron.common.log.layout; | ||
|
||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.encoder.PatternLayoutEncoder; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.Context; | ||
import ch.qos.logback.core.Layout; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class MultiLayoutPattern extends PatternLayoutEncoder { | ||
private List<Rule> rules = new ArrayList<>(); | ||
private Map<String, Layout<ILoggingEvent>> layoutMap = new HashMap<>(); | ||
|
||
public void addRule(Rule rule) { | ||
this.rules.add(rule); | ||
rule.start(context); | ||
} | ||
|
||
public byte[] encode(ILoggingEvent event) { | ||
Layout<ILoggingEvent> layout = getLayout(event.getLoggerName()); | ||
String txt = layout.doLayout(event); | ||
return convertToBytes(txt); | ||
} | ||
|
||
private byte[] convertToBytes(String s) { | ||
Charset charset = getCharset(); | ||
if (charset == null) { | ||
return s.getBytes(); | ||
} else { | ||
return s.getBytes(charset); | ||
} | ||
} | ||
|
||
private Layout<ILoggingEvent> getLayout(final String name) { | ||
if (name == null) { | ||
throw new IllegalArgumentException("name cannot be null"); | ||
} | ||
if (Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(name)) { | ||
return this.getLayout(); | ||
} | ||
if (layoutMap.containsKey(name)) { | ||
return layoutMap.get(name); | ||
} | ||
Layout<ILoggingEvent> layout = getLayout(); | ||
for (Rule rule : rules) { | ||
if (rule.logger.equals(name)) { | ||
layout = rule.getPatternLayoutEncoder().getLayout(); | ||
break; | ||
} | ||
} | ||
layoutMap.put(name, layout); | ||
return layout; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
super.start(); | ||
} | ||
|
||
public static class Rule { | ||
private String logger; | ||
private String pattern; | ||
private PatternLayoutEncoder patternLayoutEncoder; | ||
private boolean outputPatternAsHeader = false; | ||
|
||
public String getLogger() { | ||
return logger; | ||
} | ||
|
||
public void setLogger(String logger) { | ||
this.logger = logger; | ||
} | ||
|
||
public String getPattern() { | ||
return pattern; | ||
} | ||
|
||
public void setPattern(String pattern) { | ||
this.pattern = pattern; | ||
} | ||
|
||
public boolean isOutputPatternAsHeader() { | ||
return outputPatternAsHeader; | ||
} | ||
|
||
public void setOutputPatternAsHeader(boolean outputPatternAsHeader) { | ||
this.outputPatternAsHeader = outputPatternAsHeader; | ||
} | ||
|
||
public PatternLayoutEncoder getPatternLayoutEncoder() { | ||
return patternLayoutEncoder; | ||
} | ||
|
||
public void start(Context context) { | ||
patternLayoutEncoder = new PatternLayoutEncoder(); | ||
patternLayoutEncoder.setPattern(pattern); | ||
patternLayoutEncoder.setContext(context); | ||
patternLayoutEncoder.setOutputPatternAsHeader(outputPatternAsHeader); | ||
patternLayoutEncoder.start(); | ||
} | ||
} | ||
} |