Skip to content

Commit

Permalink
option to log source position
Browse files Browse the repository at this point in the history
  • Loading branch information
OutOfBedlam committed Jun 22, 2022
1 parent 8b6ceae commit deb9bbd
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lazy val root = (project in file("."))
"org.slf4j" % "slf4j-api" % "1.7.36" % Provided,
"com.typesafe.scala-logging" %% "scala-logging" % "3.9.5" % Test,
"org.scalatest" %% "scalatest" % "3.2.12" % Test,
"ch.qos.logback" % "logback-classic" % "1.2.11" % Test,
"ch.qos.logback" % "logback-classic" % "1.2.11" % Provided,
)
)
.settings(
Expand Down
55 changes: 55 additions & 0 deletions src/main/scala/com/thing2x/smqd/logging/LogLevelCounter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2019 UANGEL
//
// 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.thing2x.smqd.logging

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.filter.Filter
import ch.qos.logback.core.spi.FilterReply

import java.util.concurrent.atomic.AtomicLong

object LogLevelCounter {
val warnings = new AtomicLong()
val errors = new AtomicLong()
}

/**
* configuration example
*
* {{{
* <configuration>
* <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
*
* <filter class="com.thing2x.smqd.logging.LogLevelCounter" />
*
* <encoder>
* <pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern>
* </encoder>
* </appender>
* </configuration>
* }}}
*/
class LogLevelCounter extends Filter[ILoggingEvent]{
override def decide(event: ILoggingEvent): FilterReply = {
event.getLevel match {
case Level.WARN => LogLevelCounter.warnings.incrementAndGet()
case Level.ERROR => LogLevelCounter.errors.incrementAndGet()
case _ =>
}
FilterReply.NEUTRAL
}
}

37 changes: 37 additions & 0 deletions src/main/scala/com/thing2x/smqd/logging/LoggingBase.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019 UANGEL
//
// 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.thing2x.smqd.logging

import org.slf4j.LoggerFactory

trait SourcePositionLogging extends SourcePositionAware {
/**
* Override this if logger should write message with source code position(file name + line number)
*/
protected val logSourcePosition: Boolean = false

protected val logger: SourcePositionLogger =
SourcePositionLogger(LoggerFactory.getLogger(getClass.getName), logSourcePosition)
}

trait LoggingBase extends SourcePositionAware {
/**
* Override this if logger should write message with source code position(file name + line number)
*/
protected val logSourcePosition: Boolean = false

protected val logger: SourcePositionLogger =
SourcePositionLogger(LoggerFactory.getLogger(getClass.getName), logSourcePosition)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import org.slf4j.{LoggerFactory, Logger => Underlying}
import scala.language.experimental.macros

object SourcePositionLogger {
def apply(underlying: Underlying): SourcePositionLogger = new SourcePositionLogger(underlying)
def apply(name: String): SourcePositionLogger = new SourcePositionLogger(LoggerFactory.getLogger(name))
def apply(clazz: Class[_]): SourcePositionLogger = new SourcePositionLogger(LoggerFactory.getLogger(clazz))
def apply(underlying: Underlying, includeSourcePosition: Boolean): SourcePositionLogger =
new SourcePositionLogger(underlying, includeSourcePosition)
def apply(name: String, includeSourcePosition: Boolean): SourcePositionLogger =
new SourcePositionLogger(LoggerFactory.getLogger(name), includeSourcePosition)
def apply(clazz: Class[_], includeSourcePosition: Boolean): SourcePositionLogger =
new SourcePositionLogger(LoggerFactory.getLogger(clazz), includeSourcePosition)
}

final class SourcePositionLogger private(val underlying: Underlying ) extends Serializable {
final class SourcePositionLogger private(val underlying: Underlying, val includeSourcePosition: Boolean )
extends Serializable {
// Error
def error(message: String): Unit = macro errorMessage
def error(message: String, cause: Throwable): Unit = macro errorMessageCause
Expand Down

0 comments on commit deb9bbd

Please sign in to comment.