-
Notifications
You must be signed in to change notification settings - Fork 19
Log API Improvements #974
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
Log API Improvements #974
Changes from 11 commits
c6c58e5
0390aff
9fe1f5d
5757ac4
1434503
019dd90
87c7b43
fb92557
466e167
d9bf16e
ca17eb5
4c6e64c
bce840b
f5a2d66
9cc80d0
ebb9788
018e64e
dd7a0f8
940f24a
3381ea9
89380c4
49b138d
70e2f65
33f00db
569f8a3
c26b06e
15dcce7
c9e9a2f
0962556
0b86924
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,4 +120,51 @@ public static void debug(String message, String topic) { | |
| public static void debug(String message, String topic, Throwable ex) { | ||
| getLogger(topic).debug(message, ex); | ||
| } | ||
|
|
||
| public static void setContext(String key, Object value) { | ||
| // Topic is ignored, also if you put something | ||
| getLogger("$").setContext(key, value); | ||
| } | ||
|
|
||
| public static void write(String message, String topic, int logLevel, Object data, boolean stackTrace) { | ||
| getLogger(topic).write(message, logLevel, data, stackTrace); | ||
| } | ||
|
|
||
| public static void write(String message, String topic, int logLevel, Object data) { | ||
| getLogger(topic).write(message, logLevel, data, false); | ||
|
||
| } | ||
|
|
||
| public static boolean isDebugEnabled() { | ||
| return getLogger().isDebugEnabled(); | ||
| } | ||
|
|
||
| public static boolean isErrorEnabled() { | ||
| return getLogger().isErrorEnabled(); | ||
| } | ||
|
|
||
| public static boolean isFatalEnabled() { | ||
| return getLogger().isFatalEnabled(); | ||
| } | ||
|
|
||
| public static boolean isInfoEnabled() { | ||
| return getLogger().isInfoEnabled(); | ||
| } | ||
|
|
||
| public static boolean isWarnEnabled() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your level constant is called WARNING, but the method is isWarnEnabled(). It’s better if the naming matches exactly (e.g. either rename the constant to WARN or the method to isWarningEnabled())
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return getLogger().isWarnEnabled(); | ||
| } | ||
|
|
||
| public static boolean isTraceEnabled() { | ||
| return getLogger().isTraceEnabled(); | ||
| } | ||
|
|
||
| public static boolean isEnabled(int logLevel) { | ||
| return getLogger().isEnabled(logLevel); | ||
| } | ||
|
|
||
| public static boolean isEnabled(int logLevel, String topic) { | ||
| return getLogger(topic).isEnabled(logLevel); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,5 +65,20 @@ public interface ILogger { | |
| * msg); } } | ||
| */ | ||
|
|
||
|
|
||
| default void setContext(String key, Object value) {} | ||
|
|
||
| default void write(String message, int logLevel, Object data, boolean stackTrace) {} | ||
|
|
||
| default boolean isFatalEnabled() { return false; } | ||
|
|
||
| default boolean isWarnEnabled() { return false; } | ||
|
|
||
| default boolean isInfoEnabled() { return false; } | ||
|
|
||
| default boolean isTraceEnabled() { return false; } | ||
|
|
||
| default boolean isEnabled(int logLevel) { return false; } | ||
|
|
||
| //boolean isEnabled(int logLevel, String topic); | ||
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,8 +38,18 @@ | |
| <groupId>org.apache.ws.security</groupId> | ||
| <artifactId>wss4j</artifactId> | ||
| <version>1.6.19</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.code.gson</groupId> | ||
| <artifactId>gson</artifactId> | ||
|
||
| <version>2.12.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.logging.log4j</groupId> | ||
| <artifactId>log4j-layout-template-json</artifactId> | ||
| <version>2.24.3</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <finalName>gxwrappercommon</finalName> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't understand this template when exactly is it loaded to be used?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is loaded here: PR #959 |
||
| "@timestamp": { | ||
| "$resolver": "timestamp", | ||
| "pattern": { | ||
| "format": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | ||
| "timeZone": "UTC" | ||
| } | ||
| }, | ||
| "ecs.version": "1.2.0", | ||
| "log.level": { | ||
| "$resolver": "level", | ||
| "field": "name" | ||
| }, | ||
| "message": { | ||
| "$resolver": "customMessage", | ||
| "stringified": true | ||
| }, | ||
| "data": { | ||
| "$resolver": "map", | ||
| "key": "data" | ||
| }, | ||
| "process.thread.name": { | ||
| "$resolver": "thread", | ||
| "field": "name" | ||
| }, | ||
| "log.logger": { | ||
| "$resolver": "logger", | ||
| "field": "name" | ||
| }, | ||
| "tags": { | ||
| "$resolver": "ndc" | ||
| }, | ||
| "error.type": { | ||
| "$resolver": "exception", | ||
| "field": "className" | ||
| }, | ||
| "error.message": { | ||
| "$resolver": "exception", | ||
| "field": "message" | ||
| }, | ||
| "error.stack_trace": { | ||
| "$resolver": "exception", | ||
| "field": "stackTrace", | ||
| "stackTrace": { | ||
| "stringified": true | ||
| } | ||
| }, | ||
| "context": { | ||
| "$resolver": "mdc", | ||
| "field": "context", | ||
| "stringified": false | ||
| }, | ||
| "stackTrace": { | ||
| "$resolver": "map", | ||
| "key": "stackTrace" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.genexus.diagnostics.core.provider; | ||
|
|
||
| import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
| import org.apache.logging.log4j.core.config.plugins.PluginFactory; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactory; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverConfig; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverFactory; | ||
|
|
||
|
|
||
| @Plugin(name = "CustomMessageFactory", category = TemplateResolverFactory.CATEGORY) | ||
|
||
| public final class CustomMessageFactory implements EventResolverFactory { | ||
| private static final CustomMessageFactory INSTANCE = new CustomMessageFactory(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now you have a package-private default constructor (implicit), plus a single INSTANCE. It’s clearer to readers if you explicitly prevent instantiation
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @PluginFactory | ||
| public static CustomMessageFactory getInstance() { | ||
| return INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return CustomMessageResolver.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomMessageResolver create(EventResolverContext context, TemplateResolverConfig config) { | ||
| return new CustomMessageResolver(config); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.genexus.diagnostics.core.provider; | ||
|
|
||
| import org.apache.logging.log4j.core.LogEvent; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.EventResolver; | ||
| import org.apache.logging.log4j.layout.template.json.resolver.TemplateResolverConfig; | ||
| import org.apache.logging.log4j.layout.template.json.util.JsonWriter; | ||
| import org.apache.logging.log4j.message.MapMessage; | ||
| import org.apache.logging.log4j.message.Message; | ||
|
|
||
|
|
||
| public class CustomMessageResolver implements EventResolver { | ||
| private static final String RESOLVER_NAME = "customMessage"; | ||
|
|
||
| CustomMessageResolver(TemplateResolverConfig config) { | ||
| } | ||
|
|
||
| static String getName() { | ||
| return RESOLVER_NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public void resolve(LogEvent logEvent, JsonWriter jsonWriter) { | ||
| Message message = logEvent.getMessage(); | ||
| if (message instanceof MapMessage) { | ||
| MapMessage<?, ?> mapMessage = (MapMessage<?, ?>) message; | ||
| Object msgValue = mapMessage.get("message"); | ||
| if (msgValue != null) { | ||
| jsonWriter.writeString(msgValue.toString()); | ||
| return; | ||
| } | ||
| } | ||
| // fallback | ||
| jsonWriter.writeString(message.getFormattedMessage()); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do something like this? To ensure type safety
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a function like this inside that enum, to be able to assign the value of the received log level.
public static LogLevel fromInt(int lvl) { for (LogLevel level : LogLevel.values()) { if (level.intValue() == lvl) { return level; } } return LogLevel.OFF; }Fix: Ensure type safety