Coder Social home page Coder Social logo

Logger wip about vapor HOT 7 CLOSED

vapor avatar vapor commented on May 5, 2024
Logger wip

from vapor.

Comments (7)

tanner0101 avatar tanner0101 commented on May 5, 2024

Great implementation so far.
I think the most Vapor way to do this would be an API like so.

Log.info("Informational log")
Log.error("something bad happened")

Where the Log class looks something like this

class Log {
  static var driver: LogDriver
  static func info(message: Any) { }
  ...etc
}

This way the logger implementation can be easily swapped out without affecting the interface for logging.

Log.driver = MyCustomLogger()

Let me know what you think.

from vapor.

mpclarkson avatar mpclarkson commented on May 5, 2024

Yep - makes sense. I will refactor it tomorrow my time.

from vapor.

mpclarkson avatar mpclarkson commented on May 5, 2024

The logger now simply includes a LoggerDriver protocol, a default ConsoleLogger which implements this protocol and a Log class with static methods for each log level.

In the Log class you can:

  • Override the default logger
  • Define which levels to log by setting [LogLevel] to the static var enabledLevels.
  • Log a Custom log level

For example:

Log.info("this is informational") //Only displays if enabledLevel contains .Info
Log.custom("this is a custom method", label: "MyLevel")

//Example output: [2016-02-21 23:14:36 +0000] INFO: Received GET request for /

The Log class:

public class Log {

    public static var driver: Logger = ConsoleLogger()
    public static var enabledLevels: [LogLevel] = LogLevel.all

    public static func verbose(message: String) {
        if Log.enabledLevels.contains(.Verbose) {
            driver.log(.Verbose, message: message)
        }
    }

 //other methods

Let me know...

from vapor.

tanner0101 avatar tanner0101 commented on May 5, 2024

This looks great. I'll test it out when I'm back on my computer.

from vapor.

tanner0101 avatar tanner0101 commented on May 5, 2024

Could you submit this code as a branch on qutheory/vapor?

from vapor.

mpclarkson avatar mpclarkson commented on May 5, 2024

done

from vapor.

mpclarkson avatar mpclarkson commented on May 5, 2024

Merged in #54

from vapor.

Related Issues (20)

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.