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

logger.info(null) throws NPE when publishing #86

Open
meganalnico opened this issue May 11, 2022 · 2 comments
Open

logger.info(null) throws NPE when publishing #86

meganalnico opened this issue May 11, 2022 · 2 comments

Comments

@meganalnico
Copy link

meganalnico commented May 11, 2022

logger.info(null) throws NPE when publishing with version 1.6

java.lang.NullPointerException: null at com.internetitem.logback.elasticsearch.ClassicElasticsearchPublisher
.serializeCommonFields(ClassicElasticsearchPublisher.java:37)

Also es-logger reports this:

2022-05-10 17:48:27,075 WARN  [es-writer-6] es-error-logger: 
Failed to send events to Elasticsearch: Got response code [400] from server with data 
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception","reason":"
The bulk request must be terminated by a newline [\\n]"},"status":400}

ClassicElasticsearchPublisher line 37

String formattedMessage = event.getFormattedMessage();
if (this.settings.getMaxMessageSize() > 0 && formattedMessage.length() > this.settings.getMaxMessageSize()) {
    formattedMessage = formattedMessage.substring(0, this.settings.getMaxMessageSize()) + "..";
} 

It's very possible that a value is unexpectedly null. One might fix it thus:

String formattedMessage = String.valueOf(event.getFormattedMessage());

This way in the case of a null value the string "null" is placed in the message.

@meganalnico
Copy link
Author

meganalnico commented May 11, 2022

I have an even better fix. This way, no matter what happens you at least have valid JSON.

private void serializeEvent(JsonGenerator gen, T event, List<AbstractPropertyAndEncoder<T>> propertyList) throws IOException {
    try {
        gen.writeStartObject();
        
        serializeCommonFields(gen, event);
    	for (AbstractPropertyAndEncoder<T> pae : propertyList) {
        	propertySerializer.serializeProperty(gen, event, pae);
	}
    } finally {
	gen.writeEndObject();
    }
}

@gavenkoa
Copy link

Probably the same: #63

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

No branches or pull requests

2 participants