Log

1.1.0

Log is a lightweight logging framework written in Swift.
space-code/log

What's New

1.1.0

2024-01-19T10:21:39Z

Released on 2024-01-19

Added

  • Make the logLevel property changeable
    • Added in Pull Request #5.
  • Add files to comply with community standards
    • Added in Pull Request #4.
  • Update GitHub Actions workflow
    • Added in Pull Request #3.
  • Hide IOSWriter & IConsoleWriter from the public interface
    • Added in Pull Request #2.

Log: A lightweight logging framework written in Swift

log

License Swift Compatibility Platform Compatibility CI CodeCov GitHub release; latest by date GitHub commit activity

Description

Log is a lightweight logging framework written in Swift.

Usage

Create a logger instance

First, you need to create an instance of IPrinter that prints messages to a specific output, such as XCode's console or the Console app. The log package provides predefined printers for printing messages in the XCode console (ConsolePrinter) and the system console (OSPrinter). You can also create your own printer. To do this, your object must conform to IPrinterStrategy and implement the necessary methods.

import Log

let osPrinter = OSPrinter()
let consolePrinter = ConsolePrinter()

Second, create a Logger instance and pass these printers as initialization parameters while defining a log level. The log level determines the level of log messages to print. If the log level is set to a specific level, all messages with different log levels will be ignored. To print all messages, use .all.

let log = Logger(
    printers: [osPrinter, consolePrinter],
    logLevel: .all
)
log.error(message: "test message")

Formatting a message

Each instance of IPrinter has an array of formatters that are responsible for formatting input messages. The log package provides predefined prefix and timestamp formatters. To use these, you need to pass them to an initializer of a printer.

let osPrinter = OSPrinter(formatters: [PrefixFormatter(name: "your prefix here")])
...
log.fault(message: "message") // "🚨🚨🚨 [your prefix here] => message"

Here is a list of predefined formatters:

Formatters Description
PrefixLogFormatter Add a specified prefix to a printed message
TimestampLogFormatter Add a timestamp before a printed message based on a date format

Custom formatters

If you want to create a custom message formatter, your object must conform to the ILogFormatter protocol and implement the necessary methods.

struct MyCustomMessageFormatter: ILogFormatter {
    func format(message: String, with logLevel: LogLevel) -> String {
        // your implementation here
    }
}

Requirements

  • iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 7.0+ / visionOS 1.0+
  • Xcode 14.0
  • Swift 5.7

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but log does support its use on supported platforms.

Once you have your Swift package set up, adding log as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/space-code/log.git", .upToNextMajor(from: "1.0.0"))
]

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Contributing

Bootstrapping development environment

make bootstrap

Please feel free to help out with this project! If you see something that could be made better or want a new feature, open up an issue or send a Pull Request!

Author

Nikita Vasilev, nv3212@gmail.com

License

log is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 04:12:16 GMT-0900 (Hawaii-Aleutian Daylight Time)