Skip to content

Commit

Permalink
Merge pull request #4 from agorapulse/fix/npe-in-sentry-factory
Browse files Browse the repository at this point in the history
fixed NPE in SentryClientFactory
  • Loading branch information
musketyr committed Oct 22, 2020
2 parents 83705cd + 5f13ece commit c9fad45
Show file tree
Hide file tree
Showing 16 changed files with 460 additions and 36 deletions.
2 changes: 2 additions & 0 deletions docs/guide/src/docs/asciidoc/installation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ and replace it with a new file `log4j2.xml` with following content. Feel free to
include::../../../../../subprojects/micronaut-log4aws/src/test/resources/log4j2.xml[lines=1;21..51]
----

TIP: The library can configure `Sentry` appender for you if you forget to specify it in the configuration file.

As a last step you need to create a file `sentry.properties` for sentry configuration - also in `src/main/resources`:

.sentry.properties
Expand Down
13 changes: 0 additions & 13 deletions examples/micronaut-log4aws-demo/micronaut-log4aws-demo.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ plugins {
id "com.github.johnrengelman.shadow"
}

config {
bintray {
enabled = true
}
}

dependencies {
compile project(':micronaut-log4aws')

Expand All @@ -37,25 +31,18 @@ dependencies {

annotationProcessor "io.micronaut:micronaut-inject-java"

compile "io.sentry:sentry-log4j2:$sentryVersion"
compile group: 'com.amazonaws', name: 'aws-lambda-java-log4j2', version: awsLog4jVersion
compile "org.apache.logging.log4j:log4j-slf4j18-impl:$log4jVersion"

compile "io.micronaut:micronaut-core"
compile "io.micronaut:micronaut-function"

compileOnly "io.micronaut:micronaut-inject-groovy"



testAnnotationProcessor "io.micronaut:micronaut-inject-java"

testCompile "io.micronaut:micronaut-inject-groovy"
testCompile "com.github.stefanbirkner:system-rules:$systemRulesVersion"
testCompile("org.spockframework:spock-core") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}

}


Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ buildscript {
apply plugin: 'org.kordamp.gradle.settings'

projects {
directories = ['subprojects', 'docs', 'examples']
directories = ['subprojects', 'docs', 'examples', 'testprojects']
}

rootProject.name = 'micronaut-log4aws-root'
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
import io.sentry.log4j2.SentryAppender;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;
import java.util.Map;
import java.util.function.Supplier;

import static io.sentry.DefaultSentryClientFactory.ASYNC_OPTION;

/**
* Initializes Sentry for Micronaut.
*/
@Factory
public class SentryClientFactory {
public class Log4AwsFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(SentryClientFactory.class);
private static final String APPENDER_NAME = "Sentry";
private static final Logger LOGGER = LoggerFactory.getLogger(Log4AwsFactory.class);

/**
* Automatically registers sentry logging during application context startup.
Expand All @@ -64,6 +65,24 @@ public SentryAppender sentryAppender() {
LOGGER.error("Sentry not configured correctly for synchornous calls! Please, create file 'sentry.properties' and add there a line 'async=false'");
}

return initializeAppenderIfMissing(
SentryAppender.class,
Level.WARN,
SentryAppender.APPENDER_NAME,
SentryAppender::new
);
}

/**
* Sentry client to be injected.
*
* Please, use the injection instead of static reference to simplify testing.
*
* @return sentry client
*/
@Bean
@Context
public SentryClient sentryClient() {
Sentry.init();

SentryClient client = Sentry.getStoredClient();
Expand All @@ -82,36 +101,43 @@ public void onSuccess(Event event) {
}
});

return client;
}

private <A extends Appender> A initializeAppenderIfMissing(
Class<A> appenderClass,
Level defaultLevel,
String defaultName,
Supplier<A> initializer
) {
LoggerContext lc = (LoggerContext) LogManager.getContext(false);
Configuration configuration = lc.getConfiguration();

SentryAppender appender = configuration.getAppender(APPENDER_NAME);
String name = defaultName;
A appender = null;

for (Map.Entry<String, Appender> e : configuration.getAppenders().entrySet()) {
if (appenderClass.isInstance(e.getValue())) {
appender = appenderClass.cast(e.getValue());
name = e.getKey();
}
}

if (appender == null) {
appender = new SentryAppender();
appender = initializer.get();
appender.start();
configuration.addAppender(appender);
}
configuration.addAppender(appender);

LoggerConfig rootLoggerConfig = configuration.getRootLogger();
rootLoggerConfig.removeAppender(APPENDER_NAME);
rootLoggerConfig.addAppender(configuration.getAppender(APPENDER_NAME), Level.WARN, null);
if (rootLoggerConfig.getAppenders().values().stream().noneMatch(appenderClass::isInstance)) {
rootLoggerConfig.removeAppender(name);
rootLoggerConfig.addAppender(configuration.getAppender(name), defaultLevel, null);
}

lc.updateLoggers();

return appender;
}

/**
* Sentry client to be injected.
*
* Please, use the injection instead of static reference to simplify testing.
*
* @return sentry client
*/
@Bean
@Singleton
public SentryClient sentryClient() {
return Sentry.getStoredClient();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id 'groovy'
}

dependencies {
compile project(':micronaut-log4aws')
testCompile project(':micronaut-log4aws-test')

annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
compile platform("io.micronaut:micronaut-bom:$micronautVersion")
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
testCompile platform("io.micronaut:micronaut-bom:$micronautVersion")

annotationProcessor "io.micronaut:micronaut-inject-java"
compile "io.micronaut:micronaut-core"
compileOnly "io.micronaut:micronaut-inject-groovy"
testCompile "io.micronaut:micronaut-inject-groovy"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.log4aws.test.lambdaonly

import com.agorapulse.micronaut.log4aws.test.Log4AwsFactorySpec

class LambdaOnlySpec extends Log4AwsFactorySpec {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-License-Identifier: Apache-2.0
Copyright 2020 Agorapulse.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Configuration packages="org.apache.logging.log4j.core,com.amazonaws.services.lambda.runtime.log4j2">
<Appenders>
<Lambda name="Lambda">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n"/>
</Lambda>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Lambda"/>
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
plugins {
id 'groovy'
}

dependencies {
compile project(':micronaut-log4aws')
testCompile project(':micronaut-log4aws-test')

annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
compile platform("io.micronaut:micronaut-bom:$micronautVersion")
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
testCompile platform("io.micronaut:micronaut-bom:$micronautVersion")

annotationProcessor "io.micronaut:micronaut-inject-java"
compile "io.micronaut:micronaut-core"
compileOnly "io.micronaut:micronaut-inject-groovy"
testCompile "io.micronaut:micronaut-inject-groovy"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.micronaut.log4aws.test.lowercase

import com.agorapulse.micronaut.log4aws.test.Log4AwsFactorySpec

class LowerCaseSpec extends Log4AwsFactorySpec {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
SPDX-License-Identifier: Apache-2.0
Copyright 2020 Agorapulse.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<Configuration packages="org.apache.logging.log4j.core,io.sentry.log4j2,com.amazonaws.services.lambda.runtime.log4j2">
<Appenders>
<Lambda name="Lambda">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n"/>
</Lambda>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Sentry name="sentry"/>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Lambda"/>
<!-- Note that the Sentry logging threshold is overridden to the WARN level -->
<AppenderRef ref="sentry" level="warn"/>
</Root>
<Logger name="io.sentry" level="warn" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="software.amazon" level="warn" additivity="false">
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>
Loading

0 comments on commit c9fad45

Please sign in to comment.