Skip to content

Support for log helper methods showing the caller's line number? #11

@dlangdon

Description

@dlangdon

Is there any way to do something like this?

Reference:

http://stackoverflow.com/questions/1486233/java-logging-show-the-source-line-number-of-the-caller-not-the-logging-helper

Example use case:

  case class ExtendedTry[T](t: Try[T]) {

    /** Logs a custom message in case of a failure of a given type, as a warning.
      * @param message The custom message to be logged.
      * @param log The logger instance is received implicitly or explicitly, such that it corresponds to the caller class.
      * @tparam E The type of exception to be matched against.
      * @return The same Try[T] instance, allowing it to chain operations.
      */
    def logFailure[E <: Throwable : ClassTag](message: String, withTrace: Boolean = false)(implicit log: Logger)
    : Try[T] = {
      t.recover {
        case failure: E => {
            // Call here will print the file of the implicit Logger, but will print the line number in this file.
            if(withTrace) log.warn(failure)(message) else log.warn(s"$message : ${failure.getMessage}")
        }
      }
      t
    }
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions