Skip to content
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

Allow subclasses to insert custom fields #151

Merged
merged 1 commit into from
Oct 19, 2021

Conversation

echatman
Copy link
Contributor

Protected method that subclasses can override to insert arbitrary custom fields in the logback encoder. #147

Protected method that subclasses can override to insert arbitrary custom fields in the logback encoder. elastic#147
@github-actions github-actions bot added agent-java community Issues and PRs created by the community triage Issues and PRs that need to be triaged labels Oct 18, 2021
@apmmachine
Copy link
Contributor

apmmachine commented Oct 18, 2021

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2021-10-19T06:25:44.818+0000

  • Duration: 5 min 23 sec

  • Commit: 3ca0dca

Test stats 🧪

Test Results
Failed 0
Passed 171
Skipped 0
Total 171

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@felixbarny
Copy link
Member

/test

@felixbarny felixbarny enabled auto-merge (squash) October 19, 2021 06:29
@felixbarny felixbarny linked an issue Oct 19, 2021 that may be closed by this pull request
@felixbarny felixbarny merged commit 2595ee9 into elastic:master Oct 19, 2021
@felixbarny
Copy link
Member

Thank you!

@felixbarny felixbarny removed the triage Issues and PRs that need to be triaged label Oct 19, 2021
@lbuyse
Copy link

lbuyse commented Jul 12, 2023

Hello ,

I need exactly the same functionality. Allthough i can't get my custom EcsEncoder extension class to work with spring boot and logback.
Can i find an example of class like this somewhere ... ? i am wondering if overriding the addCustomFields method is enough.

Many thanks

@echatman
Copy link
Contributor Author

echatman commented Jul 12, 2023

Yes you just create a class that extends EcsEncoder and override the addCustomFields method to add in whatever you need. Then you use it by defining your own custom appender - you can copy from ecs-console-appender.xml or ecs-file-appender.xml and change the appender name and encoder class.

So a full logback-spring.xml config could look like this:

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="co/elastic/logging/logback/boot/ecs-console-appender.xml" />
    <appender name="CUSTOM_ECS_JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="com.echatman.MyCustomEcsEncoder">
            <serviceName>${SERVICE_NAME:-spring-boot-application}</serviceName>
            <!-- any other properties you want to set on your custom class go here -->
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CUSTOM_ECS_JSON_CONSOLE" />
    </root>
</configuration>

@lbuyse
Copy link

lbuyse commented Jul 12, 2023

Hello ,
Thats exactly what i did but while debugging i've realized i wrote a bug in my custom encoder which caused a nullpointer in my encoder that caused it not to write logs. I solved it and it works as expected , many thanks for your help and sorry to waste your time.
Kind regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-java community Issues and PRs created by the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hooks for adding custom fields
4 participants