Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dlangdon opened this issue Mar 2, 2016 · 1 comment
Open

Comments

@dlangdon
Copy link

dlangdon commented Mar 2, 2016

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
    }
 }
@lefou
Copy link

lefou commented Feb 1, 2018

Maybe, https://github.com/lihaoyi/sourcecode is a good candidate to provide such a feature. Notice, that sourcecode is also implemented by macros, thus it should not add to the runtime costs at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants