Giter Club home page Giter Club logo

hmrc.logging's Introduction

logging

Build Status Download

This simple API provides a facade for SLF4J (yes I know that's also a facade). The value of this API is that the logging operations are easily TDD testable.

  1. Classes that need to use logging should receive a SimpleLogger via dependency injection.
  2. Wrap the underlying SLF4J logger in a LoggerFacade and inject this.
  3. A test stub, StubLogger, is provided for you to use in your unit tests instead. You can inspect its content to verify that logging operations happened as expected.

There is (deliberately) no conditional logging provided here, although it still works at the underlying Logback output level. The cost of message formatting is normally relatively low. Pass expensive objects via the varargs parameter, in the manner normal for SLF4J, to avoid them being unnecessarily converted to strings when logging levels are low.

The StubLogger captures messages and arguments in LogEntry, which tests can inspect. Avoid problems with testing context-dependent values (e.g. time taken) by passing them in the argument list; the exact message contents can then be verified without recourse to fragile regular expressions etc. With this in mind, use the heuristic:

  • for values that are predictable, use Scala interpolation log.info(s"Opened $file")
  • for values that are not predictable, use varags log.info("Parsing took {}.", timeTaken)
  • blend these two as appropriate.

In each case, the StubLogger allows your tests to verify the message strings simply by testing equality, e.g.

    log.info(s"LOOKUP origin=$origin postcode=$postcode matches=$matches took {}", timeTaken)

then

    assert(log.size === 1)
    assert(log.infos.head.message === "LOOKUP origin=xyz postcode=BF1 0AX matches=1 took {}")

Hint

As an alternative testable solution for TDD development, you might also consider opslogger.

License

Except for specified exceptions, this code is open source software licensed under the Apache 2.0 License. DiagnosticTimer is licensed under the MIT Licence.

hmrc.logging's People

Contributors

alijy avatar altovedo avatar hmrc-web-operations avatar thomasrynnehmrc avatar tomasz-rosiek 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.