stevedlawrence commented on code in PR #954: URL: https://github.com/apache/daffodil/pull/954#discussion_r1101421269
########## 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: I actually based this off of the `SimpleLogger`, which also uses the `LegacyAbstractLogger`. The only difference between `AbstractLogger` and `LegacyAbstractLogger` is that it implements the `is<Level>Enabled` functions that accepts a `Marker`, and calls the `is<Level>Enabled` function that doesn't accept a `Marker`. https://github.com/qos-ch/slf4j/blob/master/slf4j-api/src/main/java/org/slf4j/helpers/LegacyAbstractLogger.java I imagine this is called "legacy" because slf4j didn't always support markers. We don't use markers in our daffodil logging, so if we extended AbstractLogger instead we would just have to implement the same code that LegacyAbstractLogger implements. I'll add a comment to make this clear in the code. Agreed that when "legacy" shows up it's often a red flag. -- 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]
