Git Product home page Git Product logo

scala-logging's Introduction

Scala Logging

Scala Logging is a convenient and performant logging library wrapping logging libraries like SLF4J and potentially others.

It's convenient, because you can simply call log methods without checking whether the respective log level is enabled:

logger.debug(s"Some $expensive message!")

It's performant, because thanks to Scala macros the check-enabled-idiom is applied, just like writing this more involved code:

if (logger.isDebugEnabled) logger.debug(s"Some $expensive message!")

Prerequisites

  • Java 6 or higher
  • Scala 2.11
  • Logging backend compatible with SLF4J, e.g. Logback

Getting Scala Logging

Scala Logging is published to Sonatype OSS and Maven Central:

  • Group id / organization: com.typesafe.scala-logging
  • Modules (artifact id / name):
    • scala-logging-api if you just need the API
    • scala-logging-slf4j for the SLF4J-backed implementation
  • Latest release version is 2.0.2

The following example shows how to add a dependency to the latest version of Scala Logging to your sbt build definition:

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.0.2"

Using Scala Logging

API

Scala Logging comes with a thin API layer, packaged in the scala-logging-api module, which is independent of a particular logging library. The com.typesafe.scalalogging package contains the following members:

The Logger trait declares abstract logging methods like error, info, etc.

The Logging trait declares the abstract logger member of type Logger, i.e. requires the class, into which this trait is mixed, to define a Logger.

SLF4J-backed implementation

The implementation for SLF4J is packaged in the scala-logging-slf4j module. The com.typesafe.scalalogging.slf4j package contains the following members:

The Logger class implements the Logger trait from the API package. It wraps an underlying SLF4J logger. Hence, in order to create a Logger, you have to pass a SLF4J logger to the apply factory method defined in the Logger companion object:

val logger = Logger(LoggerFactory getLogger "name")

The LazyLogging and StrictLogging traits implement the Logging trait from the API package. They define the logger member as a lazy or strict value respectively. In both cases the underlying SLF4J logger is named like the class into which these traits are mixed:

class MyClass extends LazyLogging {
  logger.debug("This is very convenient ;-)")
}

Contribution policy

Contributions via GitHub pull requests are gladly accepted from their original author. Before we can accept pull requests, you will need to agree to the Typesafe Contributor License Agreement online, using your GitHub account.

License

This code is open source software licensed under the Apache 2.0 License.

scala-logging's People

Watchers

Chris Birchall avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.