Giter Club home page Giter Club logo

dllogging's Introduction

DLLogging

CI Version License Platform Carthage compatible

An abstract Logging Framework supports:

  • Unified Logging.
  • Modularize, Centralize Logging.
  • Plugin the new logging easier.
  • Fully customize format logging's message.
  • Built-in Loggings
    • Console Logging
      • PrintLogging: Swift's print.
      • PrintDebugLogging: Swift's debugPrint.
    • File Logging
      • Write the log message to file.
      • It flushes the content as Data with a UTF-8 encoding and call back to client for process each configured TimeInterval and clear content.

Log Level supports

  1. ๐Ÿ—ฃ Verbose: A verbose message, usually useful when working on a specific problem.
  2. ๐Ÿ” Debug: A debug message that may be useful to a developer.
  3. โ„น๏ธ Info: An info message that highlight the progress of the application at coarse-grained level.
  4. โš ๏ธ Warning: A warning message, may indicate a possible error.
  5. โ—๏ธ Error: An error occurred, but it's recoverable, just info about what happened.
  6. ๐Ÿ›‘ Severe: A server error occurred.

Requirements

  • Xcode 11+
  • Swift 5.0+

How

Setup

  1. Use Framework's default setup.
import UIKit
import DLLogging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        /// Setup Logging
        LoggerManager.sharedInstance.initialize()
        return true
    }
}
  1. Use supported Loggings.
import UIKit
import DLLogging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        /// Setup Logging
        let logFormatter = LogFormatterImpl()
        LoggerManager.sharedInstance.addLogging(LoggerFactoryImpl.makeConsoleLogging(logFormatter: logFormatter))
        LoggerManager.sharedInstance.addLogging(LoggerFactoryImpl.makeFileLogging(fileName: "logs"))
        /// Disable LogLevels. Enable all LogLevels by default
        LoggerManager.sharedInstance.disableLogLevels([LogLevel.info, LogLevel.error])

        return true
    }
}
  1. Add your custom Logging.
import UIKit
import DLLogging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        /// Setup Logging
        let logFormatter = LogFormatterImpl()
        let testLogging = TestLogging(logFormatter: logFormatter)
        LoggerManager.sharedInstance.addLogging(testLogging)
        return true
    }
}

/// Your custom Logging.
final class TestLogging: BaseLogging {
    let logger = OSLog.init(subsystem: "com.domain.loggingdemo", category: "main")  
    override func receiveMessage(_ message: LogMessage) {
        if let formattedMessage = logFormatter?.formatMessage(message) {
            os_log("%@", log: logger, type: OSLogType.debug, formattedMessage)
        } else {
            os_log("Your message %@", message.text)
        }
    }
}
  1. Add your custom Formatter.
import UIKit
import DLLogging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        /// Setup Logging
        let logFormatter = CustomLoggingFormatter()
        let testLogging = LoggerFactoryImpl.makeConsoleLogging(logFormatter: logFormatter)
        LoggerManager.sharedInstance.addLogging(testLogging)
        return true
    }
}

/// Your custom Formatter
final class CustomLoggingFormatter: LogFormatter {
    func formatMessage(_ message: LogMessage) -> String {
        return "[\(message.level.symbol)][\(message.function)] -> \(message.text)"
    }
}

Use

/// Invoke
Log.info(message: "info")
Log.debug(message: "debug")
Log.verbose(message: "verbose")
Log.warning(message: "warning")
Log.error(message: "error")
Log.severe(message: "severe")

/// Output
2020-07-16T18:50:09.254+0700 [โ„น๏ธ][ViewController.swift:18:viewDidLoad()] -> info
2020-07-16T18:50:09.256+0700 [๐Ÿ”][ViewController.swift:19:viewDidLoad()] -> debug
2020-07-16T18:50:09.256+0700 [๐Ÿ—ฃ][ViewController.swift:20:viewDidLoad()] -> verbose
2020-07-16T18:50:09.257+0700 [โš ๏ธ][ViewController.swift:21:viewDidLoad()] -> warning
2020-07-16T18:50:09.257+0700 [โ—๏ธ][ViewController.swift:22:viewDidLoad()] -> error
2020-07-16T18:50:09.257+0700 [๐Ÿ›‘][ViewController.swift:23:viewDidLoad()] -> severe

Installation

There are three ways to install DLLogging

CocoaPods

Just add to your project's Podfile:

pod 'DLLogging', '~> 1.2'

Carthage

Add following to Cartfile:

github "lengocduy/DLLogging" ~> 1.2
  • To building platform-independent xcframeworks xcode12 and above here
  • To migrating from framework bundles to xcframework here

Swift Package Manager

Create a Package.swift file:

// swift-tools-version:5.0

import PackageDescription

let package = Package(
        name: "TestLogging",

        dependencies: [
            .package(url: "https://github.com/lengocduy/DLLogging.git", from: "1.2.0"),
        ],

        targets: [
            .target(
                    name: "TestLogging",
                    dependencies: ["DLLogging"])
        ]
)

Architecture

Architecture

Interaction Flow

Interaction Flow

License

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

dllogging's People

Contributors

lengocduy avatar

Watchers

 avatar  avatar

dllogging's Issues

[Installing] failed when installing via Carthage

  • Must be tag version or specific a branch master to install (in Cartfile: github "lengocduy/swift-log" "master")
  • Error when building the target with the error Execution policy exception registration failed and was skipped: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" (in target 'Logging' from project 'Logging')

FileLogging does not add new lines between log statements

Describe the bug
Using File Logger like

LoggerManager.sharedInstance.addLogging(LoggerFactoryImpl.makeFileLogging())

creates a FileLogging.log file in Library folder but the content has only a single line containing all the log statements.

To Reproduce
Steps to reproduce the behavior:

  1. Initialise LoggerManager with FileLogging
  2. Log something ie Log.info(...)
  3. Check the log file contents
  4. See how there are no new-line breaks (\n)

Expected behavior
Each log statement should be logged in new line in the log file.

Desktop (please complete the following information):

  • OS: macOS 12.4 Monterey

Duplicate output lines in Xcode console

Describe the bug
A single log statement results in two output log lines in Xcode console

To Reproduce
Initialize logger in AppDelegate:

LoggerManager.sharedInstance.initialize()

Make log statement like:

Log.error(message: "Some error")

Xcode console output will contain:

2021-10-24T21:34:46.406+0200 [โ—๏ธ][MyClass.swift:36:main()] -> Some error
"2021-10-24T21:34:46.407+0200 [โ—๏ธ][MyClass.swift:36:main()] -> Some error"

Notice the second line has "..." quotes.

Expected behavior
A single log statement should result in single console output line.

iOS Simulator:

  • OS: iOS 15.0

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.