Skip to content

Conversation

@arVahedi
Copy link

@arVahedi arVahedi commented Aug 17, 2021

Add Enhanced JSON layout to define dynamic class as a model of the JSON structure.

Usage:

XML configuration:

<RollingRandomAccessFile name="Json-Appender">

           <EnhancedJsonLayout complete="false" compact="true" eventEol="true" 
                                    model="package.xxx.CustomModelLogEventWrapper">

           </EnhancedJsonLayout>

   </RollingRandomAccessFile>

And CustomModelLogEventWrapper class:

    public class CustomModelLogEventWrapper extends JsonModelLogEventWrapper {

    public CustomModelLogEventWrapper(LogEvent event, Map<String, String> additionalFields) {
        super(event, additionalFields);
    }

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS XXX", timezone = "Asia/Tehran")
    public Date getTime() {
        return new Date(getLogEvent().getTimeMillis());
    }

    public Level getLevel() {
        return getLogEvent().getLevel();
    }

    public String getLogger() {
        return getLogEvent().getLoggerName();
    }

    public Message getMessage() {
        return new Message(getLogEvent().getMessage().getFormattedMessage(), getLogEvent().getThrown());
    }

    public String getMachineName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "";
        }
    }

    public String getClientIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            return "";
        }
    }

    public String getThreadName() {
        return getLogEvent().getThreadName();
    }

    public long getThreadId() {
        return getLogEvent().getThreadId();
    }

    public String getActivityId() {
        if (getLogEvent().getSource() == null) {
            return "";
        }

        return getLogEvent().getSource().getClassName() + "::" + getLogEvent().getSource().getMethodName();
    }

    public String getApplicationType() {
        return "";
    }

    public StackTraceElement getSource() {
        return getLogEvent().getSource();
    }

    @Getter
    @AllArgsConstructor
    public static class Message {
        private String formattedMessage;
        private Throwable thrown;
    }
}

Signed-off-by: arVahedi ar.vahedi357@gmail.com

Add Enhanced json layout for defined dynamic class as model of json structure

Signed-off-by: arVahedi <ar.vahedi357@gmail.com>
@vy
Copy link
Member

vy commented Aug 18, 2021 via email

@arVahedi
Copy link
Author

arVahedi commented Aug 18, 2021

Why don't you simply use JsonTemplateLayout?

JsonTemplateLayout is a great feature, but it is still limited. imagine I need to log some value out of LogEvent context and based on some logic at runtime, for example generate an activity id, digest value or something like that at runtime.
The EnhancedJsonLayout offers you a full control on what you need to log.

@carterkozak
Copy link
Contributor

imagine I need to log some value out of LogEvent context and based on some logic at runtime, for example generate an activityId, digest value or something like that at runtime.

Couldn't you implement these using a custom TemplateResolver to define additional components?

@arVahedi
Copy link
Author

arVahedi commented Aug 19, 2021

imagine I need to log some value out of LogEvent context and based on some logic at runtime, for example generate an activityId, digest value or something like that at runtime.

Couldn't you implement these using a custom TemplateResolver to define additional components?

Maybe, but it is more complex way. I amaze why we have implemented some complex approach instead of define just one model class as simplest way we can to use.

@vy
Copy link
Member

vy commented Aug 19, 2021 via email

@vy
Copy link
Member

vy commented Aug 26, 2021

I have just skimmed over the changes and I am totally against this PR. We already have 3 competing JSON layouts in the project: GelfLayout, JsonLayout, and its successor JsonTemplateLayout. Putting aside we will be introducing a feature bloat to the code base, I am still yet to see a single function this new layout incorporates that is not possible with JsonTemplateLayout.

I will to close this PR and the JIRA ticket due to insufficient justification. Feel free to reopen them if you have objections.

@vy vy closed this Aug 26, 2021
@vy vy changed the title Close jira issue LOG4J2-3138 LOG4J2-3138 Define a custom model for a json layout for creating custom json logs output Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants