Swiftpack.co - bilm/Logger as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by bilm.
bilm/Logger yonder
a Logging framework
⭐️ 0
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/bilm/Logger.git", from: "yonder")

Logger

(The Lightswitch Way)

by using the stand alone functions Log(Bool,Any...) and Label(Any...) or by using the Logger.debug(Any) and its siblings


functions

the basic function are:

  • Log(Bool, Any...) -- if true, print the items to STDERR
  • Label(Any...) -- print the items to STDERR
  • Log(Error, message:Any?) -- if there is an Error, print the message or the Error description
Log(true, "bullseye")
Log(true, "scoping", "scrying", 12)
Label("targetting")
Label(12, "discovering")

enum FalseError:Int,LocalizedError { 
	case DOOM=911
	public var errorDescription: String? { return "\(rawValue) -- DOOM and GLOOM" }
}
Log(FalseError.DOOM)

"globally"

there are 7 "logging" levels: ALL, DEBUG, INFO, WARNING, ERROR, FATAL and OFF
there are 5 logging functions: debug, info, warning, error and fatal

these methods are present both as a class method and a normal instance method. there is a class variable (Level) that controls the currently visible levels that can be displayed and when set to OFF, no messages are displayed at all. for example, setting the Level to WARNING means that warning, error and fatal messages are displayed, but debug and info messages are not.

Logger.Level = .WARNING

print( Logger.Level )

Logger.debug("sayit")
Logger.info("sayit")
Logger.warning("sayit", "warning")
Logger.error("sayit")
Logger.fatal("sayit")

"locally"

when logging locally, an instance is used. this instance determines which method will (or will not) display a message.

NOTE: when the Logger.Level is set to OFF, no method will display a message.

let logger:Logger = .FATAL

logger.debug("sayitagain")
logger.info("sayitagain")
logger.warning("sayitagain")
logger.error("sayitagain")
logger.fatal("sayitagain")
print()

As a fileprivate variable

import Foundation

fileprivate let MyLogger = Logger.DEBUG

public
struct BasicLogging {
	public static func test() {
		Logger.info("everything")
		MyLogger.info("something")
	}
}

BasicLogging.test()

Using OSLog

extension OSLog {
	public static func playground(_ category:String) ->OSLog {
		return OSLog(subsystem: "com.lsi.playground", category: category)
	}
}

Logger.warning("sayit", "oslog", oslog:.playground("test"))
Logger.error("demoIt", oslog:.playground("demo"))
Logger.info("displayIt", oslog:.playground("demo"))

Requiring a minimal level

the main reason for this is when we are debugging and do not want side effects to occur when the level is higher than what is required.

logger.requires(.INFO) {
	logger in
	logger.debug("we are debuging")
	logger.info("we must informative")
	logger.warning("this is too high")
	logger.error("we are in bad shape")
	logger.fatal("we are done")
}

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics