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

Logger - new PR #54

Merged
merged 4 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 108 additions & 0 deletions Sources/Log.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//
// Log.swift
// Vapor
//
// Created by Matthew on 21/02/2016.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import Foundation

/* Log class
*/
public class Log {

/**
Logger. Default is the console logger.
This can be overriden with a custom logger.
*/
public static var driver: Logger = ConsoleLogger()

/**
Enabled log levels. Default is to log all levels. This
can be overridden.
*/
public static var enabledLevels: [LogLevel] = LogLevel.all

/**
Logs verbose messages if .Verbose is enabled

- parameter message: String to log

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

/**
Logs debug messages if .Debug is enabled

- parameter message: String to log

*/
public static func debug(message: String) {
if Log.enabledLevels.contains(.Debug) {
driver.log(.Debug, message: message)
}
}

/**
Logs info messages if .Info is enabled

- parameter message: String to log

*/
public static func info(message: String) {
if Log.enabledLevels.contains(.Info) {
driver.log(.Info, message: message)
}
}

/**
Logs warning messages if .Warning is enabled

- parameter message: String to log

*/
public static func warning(message: String) {
if Log.enabledLevels.contains(.Warning) {
driver.log(.Warning, message: message)
}
}

/**
Logs error messages if .Error is enabled

- parameter message: String to log

*/
public static func error(message: String) {
if Log.enabledLevels.contains(.Error) {
driver.log(.Error, message: message)
}
}

/**
Logs fatal messages if .Fatal is enabled

- parameter message: String to log

*/
public static func fatal(message: String) {
if Log.enabledLevels.contains(.Fatal) {
driver.log(.Fatal, message: message)
}
}

/**
Logs custom messages. Always enabled.

- parameter message: String to log

*/
public static func custom(message: String, label: String) {
driver.log(.Custom(label), message: message)
}
}
48 changes: 48 additions & 0 deletions Sources/LogLevel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// LogLevel.swift
// Vapor
//
// Created by Matthew on 22/02/2016.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import Foundation

/*
LogLevel enumeration
*/
public enum LogLevel {
case Verbose, Debug, Info, Warning, Error, Fatal, Custom(String)

/*
Returns all standard log levels (i.e. except Custom)

returns - array of LogLevel
*/
public static var all: [LogLevel] {
return [.Verbose, .Debug, .Info, .Warning, .Error, .Fatal]
}
}

//MARK: Protocol conformance

extension LogLevel: CustomStringConvertible {
public var description: String {
switch self {
case Verbose: return "VERBOSE"
case Debug: return "DEBUG"
case Info: return "INFO"
case Warning: return "WARNING"
case Error: return "ERROR"
case Fatal: return "FATAL"
case Custom(let string): return "\(string.uppercaseString)"
}
}
}

extension LogLevel: Equatable {}

public func ==(lhs: LogLevel, rhs: LogLevel) -> Bool {
return lhs.description == rhs.description
}

30 changes: 30 additions & 0 deletions Sources/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// LogDriver.swift
// Vapor
//
// Created by Matthew on 21/02/2016.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import Foundation

/* Logger protocol. Custom loggers must conform
to this protocol
*/
public protocol Logger {
func log(level: LogLevel, message: String)
}

/*
Logs to the console

- parameter level: LogLevel enum
- parameter message: String to log
*/
public class ConsoleLogger: Logger {

public func log(level: LogLevel, message: String) {
let date = NSDate()
print("[\(date)] \(level): \(message)")
}
}
2 changes: 2 additions & 0 deletions Sources/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class Request {

let query = Request.parseQueryData(path)
self.data = Data(query: query, bytes: body)

Log.verbose("Received \(method) request for \(path)")
}

/**
Expand Down
93 changes: 93 additions & 0 deletions Tests/LogTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// LogTests.swift
// Vapor
//
// Created by Matthew on 23/02/2016.
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import XCTest
//@testable import Vapor

class LogTests: XCTestCase {

class DummyLogger: Logger {

static var output: String?

func log(level: LogLevel, message: String) {
DummyLogger.output = "\(level.description) \(message)"
}
}

/* Resets the logger for each test
*/
override func setUp() {
DummyLogger.output = nil
Log.driver = DummyLogger()
Log.enabledLevels = LogLevel.all
}

func testCanOverrideDefaultLogger() {
XCTAssertTrue(String(Log.driver).containsString("DummyLogger"), "driver should be DummyLogger")
}

func testAllLevelsEnabledByDefault() {
let levels = LogLevel.all
levels.forEach { level in
XCTAssertTrue(Log.enabledLevels.contains(level))
}
}

func testCanOverrideDefaultEnabledLevels() {
Log.enabledLevels = [LogLevel.Debug]
XCTAssertTrue(Log.enabledLevels.count == 1, "only one log level should be enabled")
XCTAssertTrue(Log.enabledLevels.first == LogLevel.Debug, "only Debug logs should be enabled")
}

func testDisabledLogsDoNoOutput() {
Log.enabledLevels = [LogLevel.Debug]
Log.error("this should not output")
XCTAssertNil(DummyLogger.output, "disabled level should not output")
}

func testVerboseDidLog() {
Log.verbose("foo")
XCTAssertTrue(DummyLogger.output == "VERBOSE foo", "logger should output VERBOSE foo")
}

func testDebugDidLog() {
Log.debug("foo")
XCTAssertTrue(DummyLogger.output == "DEBUG foo", "logger should output DEBUG foo")
}

func testInfoDidLog() {
Log.info("foo")
XCTAssertTrue(DummyLogger.output == "INFO foo", "logger should output INFO foo")
}

func testWarningDidLog() {
Log.warning("foo")
XCTAssertTrue(DummyLogger.output == "WARNING foo", "logger should output WARNING foo")
}

func testErrorDidLog() {
Log.error("foo")
XCTAssertTrue(DummyLogger.output == "ERROR foo", "logger should output ERROR foo")
}

func testFatalDidLog() {
Log.fatal("foo")
XCTAssertTrue(DummyLogger.output == "FATAL foo", "logger should output FATAL foo")
}

func testCustomDidLog() {
Log.custom("foo", label: "customlog")
XCTAssertTrue(DummyLogger.output == "CUSTOMLOG foo", "logger should output CUSTOMLOG foo")
}

func testConsoleLoggerDidPrintToConsole() {
Log.driver = ConsoleLogger()
XCTAssertNotNil(Log.info("foo")) //Todo: This test isn't actually doing anything. I'm unsure how to assert console output...?
}
}
30 changes: 30 additions & 0 deletions Vapor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
288CFC321C7AC05B00E4617A /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 288CFC2D1C7AC01A00E4617A /* Application.swift */; };
288CFC331C7AC05D00E4617A /* Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 288CFC301C7AC02A00E4617A /* Provider.swift */; };
3492B2EF1C787DD600D8E588 /* RouteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3492B2EE1C787DD600D8E588 /* RouteTests.swift */; };
34CC59561C7BE3C9007CA680 /* LogTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59551C7BE3C9007CA680 /* LogTests.swift */; };
34CC595A1C7BE405007CA680 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59571C7BE405007CA680 /* Log.swift */; };
34CC595B1C7BE405007CA680 /* Log.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59571C7BE405007CA680 /* Log.swift */; };
34CC595C1C7BE405007CA680 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59581C7BE405007CA680 /* Logger.swift */; };
34CC595D1C7BE405007CA680 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59581C7BE405007CA680 /* Logger.swift */; };
34CC595E1C7BE405007CA680 /* LogLevel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59591C7BE405007CA680 /* LogLevel.swift */; };
34CC595F1C7BE405007CA680 /* LogLevel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34CC59591C7BE405007CA680 /* LogLevel.swift */; };
802030011C77C956009B8655 /* Vapor+Json.swift in Sources */ = {isa = PBXBuildFile; fileRef = 802030001C77C956009B8655 /* Vapor+Json.swift */; };
802030121C78CAC2009B8655 /* Branch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 802030101C78CAC2009B8655 /* Branch.swift */; };
802030131C78CAC2009B8655 /* Branch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 802030101C78CAC2009B8655 /* Branch.swift */; };
Expand Down Expand Up @@ -85,6 +92,10 @@
288CFC2D1C7AC01A00E4617A /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = "<group>"; };
288CFC301C7AC02A00E4617A /* Provider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Provider.swift; sourceTree = "<group>"; };
3492B2EE1C787DD600D8E588 /* RouteTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteTests.swift; sourceTree = "<group>"; };
34CC59551C7BE3C9007CA680 /* LogTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogTests.swift; sourceTree = "<group>"; };
34CC59571C7BE405007CA680 /* Log.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Log.swift; sourceTree = "<group>"; };
34CC59581C7BE405007CA680 /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
34CC59591C7BE405007CA680 /* LogLevel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LogLevel.swift; sourceTree = "<group>"; };
802030001C77C956009B8655 /* Vapor+Json.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Vapor+Json.swift"; sourceTree = "<group>"; };
802030101C78CAC2009B8655 /* Branch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Branch.swift; sourceTree = "<group>"; };
802030161C78CAE1009B8655 /* BranchRouter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BranchRouter.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -154,6 +165,16 @@
name = Providers;
sourceTree = "<group>";
};
34CC59601C7BE9C1007CA680 /* Log */ = {
isa = PBXGroup;
children = (
34CC59571C7BE405007CA680 /* Log.swift */,
34CC59581C7BE405007CA680 /* Logger.swift */,
34CC59591C7BE405007CA680 /* LogLevel.swift */,
);
name = Log;
sourceTree = "<group>";
};
80202FFF1C77C94C009B8655 /* Json */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -258,6 +279,7 @@
C352E6901C62BB1C00E26467 /* Tests */ = {
isa = PBXGroup;
children = (
34CC59551C7BE3C9007CA680 /* LogTests.swift */,
C352E6931C62BB1C00E26467 /* Info.plist */,
C38808631C62C0390067DADD /* ResponseTests.swift */,
C3943DC21C7663E80014F4EE /* RouterTests.swift */,
Expand All @@ -269,6 +291,7 @@
C352E6D11C62BC6A00E26467 /* Sources */ = {
isa = PBXGroup;
children = (
34CC59601C7BE9C1007CA680 /* Log */,
80202FFF1C77C94C009B8655 /* Json */,
288CFC2D1C7AC01A00E4617A /* Application.swift */,
C377F2ED1C696C9B00DABD21 /* View */,
Expand Down Expand Up @@ -438,9 +461,11 @@
C352E7011C62BC6A00E26467 /* Socket.swift in Sources */,
C3EDEA7D1C683CE100754727 /* SessionMiddleware.swift in Sources */,
C304C8AA1C62FCA300058C92 /* RequestFormExtension.swift in Sources */,
34CC595E1C7BE405007CA680 /* LogLevel.swift in Sources */,
C352E6F31C62BC6A00E26467 /* Request.swift in Sources */,
802053991C7A8C7D009E26E9 /* RequestData+Target.swift in Sources */,
C304C8B51C6306B100058C92 /* RouterDriver.swift in Sources */,
34CC595C1C7BE405007CA680 /* Logger.swift in Sources */,
C3EDEA871C68459300754727 /* Hash.swift in Sources */,
C352E7091C62BC6A00E26467 /* View.swift in Sources */,
288CFC2E1C7AC01A00E4617A /* Application.swift in Sources */,
Expand All @@ -461,6 +486,7 @@
288CFC311C7AC02A00E4617A /* Provider.swift in Sources */,
C352E6FD1C62BC6A00E26467 /* Server.swift in Sources */,
802030171C78CAE1009B8655 /* BranchRouter.swift in Sources */,
34CC595A1C7BE405007CA680 /* Log.swift in Sources */,
C3EDEA841C6842C700754727 /* Int+Random.swift in Sources */,
C352E6F11C62BC6A00E26467 /* SocketParser.swift in Sources */,
C352E7051C62BC6A00E26467 /* StringExtensions.swift in Sources */,
Expand All @@ -472,17 +498,21 @@
buildActionMask = 2147483647;
files = (
C352E7021C62BC6A00E26467 /* Socket.swift in Sources */,
34CC595F1C7BE405007CA680 /* LogLevel.swift in Sources */,
34CC595D1C7BE405007CA680 /* Logger.swift in Sources */,
C352E6E81C62BC6A00E26467 /* Controller.swift in Sources */,
C304C8B61C6306B100058C92 /* RouterDriver.swift in Sources */,
C352E6EC1C62BC6A00E26467 /* LinuxFixes.swift in Sources */,
C38808641C62C0390067DADD /* ResponseTests.swift in Sources */,
C352E6F81C62BC6A00E26467 /* ResponseConvertible.swift in Sources */,
34CC595B1C7BE405007CA680 /* Log.swift in Sources */,
C352E6F01C62BC6A00E26467 /* MemorySessionDriver.swift in Sources */,
C352E7001C62BC6A00E26467 /* Session.swift in Sources */,
288CFC321C7AC05B00E4617A /* Application.swift in Sources */,
3492B2EF1C787DD600D8E588 /* RouteTests.swift in Sources */,
C3943DC41C7664D90014F4EE /* Middleware.swift in Sources */,
C352E6F61C62BC6A00E26467 /* Response.swift in Sources */,
34CC59561C7BE3C9007CA680 /* LogTests.swift in Sources */,
C3943DC31C7663E80014F4EE /* RouterTests.swift in Sources */,
C3EDEA851C6842C700754727 /* Int+Random.swift in Sources */,
802030131C78CAC2009B8655 /* Branch.swift in Sources */,
Expand Down