stevedlawrence commented on code in PR #954: URL: https://github.com/apache/daffodil/pull/954#discussion_r1101441117
########## daffodil-slf4j-logger/src/main/scala/org/apahe/daffodil/slf4j/DaffodilSLF4JLogger.scala: ########## @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.daffodil.slf4j + +import java.io.PrintStream +import java.util.concurrent.ConcurrentHashMap + +import org.slf4j.ILoggerFactory +import org.slf4j.IMarkerFactory +import org.slf4j.Logger +import org.slf4j.Marker +import org.slf4j.event.Level +import org.slf4j.helpers.BasicMarkerFactory +import org.slf4j.helpers.LegacyAbstractLogger +import org.slf4j.helpers.MessageFormatter +import org.slf4j.helpers.NOPMDCAdapter +import org.slf4j.spi.MDCAdapter +import org.slf4j.spi.SLF4JServiceProvider + +class DaffodilSLF4JServiceProvider extends SLF4JServiceProvider { + override lazy val getLoggerFactory: ILoggerFactory = new DaffodilLoggerFactory() + override lazy val getMarkerFactory: IMarkerFactory = new BasicMarkerFactory() + override lazy val getMDCAdapter: MDCAdapter = new NOPMDCAdapter() + override lazy val getRequestedApiVersion: String = "2.0.99" + override def initialize(): Unit = {} +} + +class DaffodilLoggerFactory extends ILoggerFactory { + + private val loggerMap = new ConcurrentHashMap[String, DaffodilLogger]() + + override def getLogger(name: String): Logger = { + val daffodilLogger = loggerMap.get(name) + if (daffodilLogger != null) { + daffodilLogger + } else { + val newInstance = new DaffodilLogger(name) + val oldInstance = loggerMap.putIfAbsent(name, newInstance) + if (oldInstance == null) { + newInstance + } else { + oldInstance + } + } + } +} + +/** + * A logger that should only be used by specifically by Daffodil (e.g. CLI, TDML + * Runner) to support thread specific log levels and log streams. This is mostly + * useful for testing where we want to allow each test thread to log at a + * different level and stream. To set the level and stream, one should call: + * + * daffodilLogger.setThreadLoggerConfig(level, stream) + * + * When done, the config should be removed by calling: + * + * daffodilLogger.removeThreadLoggerConfig() + */ +class DaffodilLogger(name: String) extends LegacyAbstractLogger { Review Comment: On second thought, it's trivial to implement those extra functions even if it's a bit of extra code. It's probably better to not have to explain why we are using a "legacy" thing and just extend AbstractLogger and implement those 5 functions ourselves. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
