Coder Social home page Coder Social logo

swift-log-file's Introduction

Swift Log File

A Swift logging backend that writes logs to files using the C fopen/fwrite APIs.

It is an implementation of a LogHandler as defined by the Swift Server Working Group logging API.

Installation (SPM)

Add the following dependency in your Package.swift

.package(url: "https://github.com/Ponyboy47/swift-log-file.git", .branch("master"))

Usage

Share a log file across loggers

During application startup:

import Logging
import FileLogging

// Create a factory which will point any newly created logs to the same file
let fileFactory = FileLogHandlerFactory(path: "/path/to/file.log")

// Initialize the file log handler
LoggingSystem.bootstrap(fileFactory.makeFileLogHandler)

Elsewhere:

// Create a logger
let logger = Logger(label: "MyApp")

// Write a log message
logger.info("Hello world!")

Create a new log file for each logger

During application startup:

import Logging
import FileLogging

// Create a factory with just a directory where logs will be created
let fileFactory = FileLogHandlerFactory(path: "/path/to/logs/directory/")

// Initialize the file log handler
LoggingSystem.bootstrap(fileFactory.makeFileLogHandler)

Elsewhere:

// Creates a new log file at /path/to/logs/directory/MyApp.log
let logger = Logger(label: "MyApp")

logger.info("Hello world!")

Rotating Handlers

This implementation includes both a date-based and size-based rotating mechanism for logs. The bootstrapping is a bit verbose, but it makes creating the loggers just as easy as what you see anywhere else.

Path

The path parameter for the RotatingFileLogHandlerFactory behaves exactly the same as the path parameter in the FileLogHandlerFactory. Specifying a file or uncreated path will be used as a FilePath while a directory will be used as a parent location for files based on the label during Logger creation.

Max

The max parameter defaults to nil. Passing a max of nil means old logs will never be deleted. It is good practice to include a maximum if you're expecting lots of logs or you can set up some other tool to clean up/offload extra files.

Date Rotating Handlers

import Logging
import FileLogging

// Create a factory that will rotate logs at midnight every day and deletes any
// logs from more than 7 days ago
let dateRotatingFactory = RotatingFileLogHandlerFactory<DateRotatingFileLogHandler>(path: "/path/to/file.log", options: .daily, max: 7)

LoggingSystem.bootstrap(dateRotatingFactory.makeRotatingFileLogHandler)

Elsewhere

let logger = Logger(label: "MyApp")

logger.info("Hello world!")

Size Rotating Handlers

import Logging
import FileLogging

// Create a factory that will rotate logs as soon as the size would exceed 100
// megabytes and only permits up to 5 rotations before deleting old log files
let sizeRotatingFactory = RotatingFileLogHandlerFactory<SizeRotatingFileLogHandler>(path: "/path/to/file.log", options: 100.megabytes, max: 5)

LoggingSystem.bootstrap(sizeRotatingFactory.makeRotatingFileLogHandler)

Elsewhere

let logger = Logger(label: "MyApp")

logger.info("Hello world!")

Todo

  • Track when we were passed FileStreams directly so we know not to close them explicitly
  • Don't use so many fatalErrors
  • Performance tests
    • Date vs TimeInterval (Date vs Double comparisons/calculations)
  • Find some shorter names for things (RotatingFileLogHandlerFactory? And it requires a generic type?)
  • Allow customizing the datetime format printed in the logs

License

MIT (c) 2019 Jacob Williams

swift-log-file's People

Contributors

oskargroth avatar ponyboy47 avatar

Stargazers

 avatar  avatar

Watchers

 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.