All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opentable.server.ServerLoggingConfiguration Maven / Gradle / Ivy

Go to download

Wiring for basic Jetty core servlet regardless of MVC or JaxRS choices. Also wires logging, metrics, etc

There is a newer version: 6.0.4
Show newest version
/*
 * 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
 *
 * http://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.opentable.server;

import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import ch.qos.logback.classic.BasicConfigurator;
import ch.qos.logback.classic.LoggerContext;

import com.opentable.logging.AssimilateForeignLogging;
import com.opentable.logging.CommonLogHolder;

/* Registered via META-INF/spring.factories to capture early application lifecycle events */
public class ServerLoggingConfiguration implements ApplicationListener {
    public static final String DMITRY_COMPONENT_NAME_KEY = "info.component"; // Taken from Dmitry's stack
    public static final String OT_COMPONENT_NAME_KEY = "ot.component.id"; // New possible standard
    private static final Logger LOG = LoggerFactory.getLogger(ServerLoggingConfiguration.class);

    static {
        AssimilateForeignLogging.assimilate();
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            ApplicationEnvironmentPreparedEvent castEvent = (ApplicationEnvironmentPreparedEvent) event;
            // Prefer OT, then Dmitry, then Manifest (see foundation basePOM
            String serviceName =
                    Optional.ofNullable(castEvent.getEnvironment().getProperty(OT_COMPONENT_NAME_KEY))
                            .orElse(Optional.ofNullable(castEvent.getEnvironment().getProperty(DMITRY_COMPONENT_NAME_KEY))
                                    .orElse(castEvent.getSpringApplication().getMainApplicationClass().getPackage().getImplementationTitle())
                            );
            LOG.info("Setting service name to {}", serviceName);
            CommonLogHolder.setServiceType(serviceName);
        }
        if (event instanceof ApplicationFailedEvent) {
            LOG.info("Terminating default logging context");
            LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
            loggerContext.reset();
            new BasicConfigurator().configure(loggerContext);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy