|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.otel.logging; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.Level; |
| 22 | +import org.apache.logging.log4j.core.config.Configurator; |
| 23 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; |
| 24 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; |
| 25 | +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; |
| 26 | + |
| 27 | +public class AgentLog { |
| 28 | + |
| 29 | + private static final String PATTERN = "%d{DEFAULT} [%t] %-5level %logger{36} - %msg{nolookups}%n"; |
| 30 | + |
| 31 | + // logger is an empty string |
| 32 | + private static final String ROOT_LOGGER_NAME = ""; |
| 33 | + |
| 34 | + private AgentLog() {} |
| 35 | + |
| 36 | + public static void init() { |
| 37 | + |
| 38 | + ConfigurationBuilder<BuiltConfiguration> conf = |
| 39 | + ConfigurationBuilderFactory.newConfigurationBuilder(); |
| 40 | + |
| 41 | + conf.add( |
| 42 | + conf.newAppender("stdout", "Console") |
| 43 | + .add(conf.newLayout("PatternLayout").addAttribute("pattern", PATTERN))); |
| 44 | + |
| 45 | + conf.add(conf.newRootLogger().add(conf.newAppenderRef("stdout"))); |
| 46 | + |
| 47 | + Configurator.initialize(conf.build(false)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the agent log level at runtime |
| 52 | + * |
| 53 | + * @param level log level |
| 54 | + */ |
| 55 | + public static void setLevel(Level level) { |
| 56 | + // Using log4j2 implementation allows to change the log level programmatically at runtime |
| 57 | + // which is not directly possible through the slf4j API and simple implementation used in |
| 58 | + // upstream distribution. |
| 59 | + |
| 60 | + Configurator.setAllLevels(ROOT_LOGGER_NAME, level); |
| 61 | + |
| 62 | + // when debugging we should avoid very chatty http client debug messages |
| 63 | + // this behavior is replicated from the upstream distribution. |
| 64 | + if (level.intLevel() >= Level.DEBUG.intLevel()) { |
| 65 | + Configurator.setLevel("okhttp3.internal.http2", Level.INFO); |
| 66 | + Configurator.setLevel("okhttp3.internal.concurrent.TaskRunner", Level.INFO); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments