├── .gitignore ├── Package.swift ├── README.md ├── Sources └── Logging.swift └── logexample ├── Package.swift └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .build 2 | .*.swp 3 | *~ 4 | Packages 5 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | import PackageDescription 2 | 3 | let package = Package( 4 | name:"swiftlog", 5 | dependencies:[ 6 | .Package(url:"https://github.com/FredLoh/Rainbow", majorVersion:1, minor:1) 7 | ] 8 | ) 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | swiftlog 2 | ======== 3 | 4 | A Logging Library to use with Swift 5 | -------------------------------------------------------------------------------- /Sources/Logging.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Logging.swift 3 | // 4 | // Copyright (c) 2014 iAchieved.it LLC. 5 | // All rights reserved. 6 | // 7 | // Redistribution and use in source and binary forms are permitted 8 | // provided that the above copyright notice and this paragraph are 9 | // duplicated in all such forms and that any documentation, 10 | // advertising materials, and other materials related to such 11 | // distribution and use acknowledge that the software was developed 12 | // by iAchieved.it LLC. The name of the 13 | // iAchieved.it LLC may not be used to endorse or promote products derived 14 | // from this software without specific prior written permission. 15 | // THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR 16 | // IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 18 | // 19 | 20 | import Foundation 21 | import Rainbow 22 | 23 | public enum SLogLevel:Int { 24 | case None = 0 25 | case Error 26 | case Warning 27 | case Info 28 | case Verbose 29 | } 30 | 31 | public var slogLevel:SLogLevel = SLogLevel.None 32 | 33 | private var slogFilePath:String? = nil 34 | 35 | public func SLogVerbose(_ logString:String, filePath:String = #file, lineNumber:Int = #line) { 36 | SLog(logLevel:.Verbose, logString:logString.green, filePath:filePath, lineNumber:lineNumber) 37 | } 38 | 39 | public func SLogInfo(_ logString:String, filePath:String = #file, lineNumber:Int = #line) { 40 | SLog(logLevel:.Info, logString:logString.white, filePath:filePath, lineNumber:lineNumber) 41 | } 42 | 43 | public func SLogWarning(_ logString:String, filePath:String = #file, lineNumber:Int = #line) { 44 | SLog(logLevel:.Warning, logString:logString.yellow, filePath:filePath, lineNumber:lineNumber) 45 | } 46 | 47 | public func SLogError(_ logString:String, filePath:String = #file, lineNumber:Int = #line) { 48 | SLog(logLevel:.Error, logString:logString.red, filePath:filePath, lineNumber:lineNumber) 49 | } 50 | 51 | public func ENTRY_LOG(functionName:String = #function, filePath:String = #file, lineNumber:Int = #line) { 52 | SLogVerbose("ENTRY " + functionName, filePath:filePath, lineNumber:lineNumber) 53 | } 54 | 55 | public func EXIT_LOG(functionName:String = #function, filePath:String = #file, lineNumber:Int = #line) { 56 | SLogVerbose("EXIT " + functionName, filePath:filePath, lineNumber:lineNumber) 57 | } 58 | 59 | public func slogToFile(atPath path:String, append:Bool = false) { 60 | let fileManager = FileManager.default() 61 | slogFilePath = path 62 | 63 | if let logFile = slogFilePath { 64 | if !append || !fileManager.fileExists(atPath:path) { 65 | _ = fileManager.createFile(atPath:logFile, contents:nil, attributes:nil) 66 | } 67 | } 68 | } 69 | 70 | func SLog(logLevel:SLogLevel, logString:String, filePath:String, lineNumber:Int) { 71 | let date = Date() 72 | let fileUrl = URL(fileURLWithPath:filePath) 73 | let log = "\(date) - \(fileUrl.lastPathComponent!):\(lineNumber) - " + stringForLogLevel(logLevel:logLevel) + " - " + logString + "\n" 74 | let appLogLevel = slogLevel.rawValue 75 | if (appLogLevel >= logLevel.rawValue) { 76 | print(log, terminator:"") 77 | if let logFilePath = slogFilePath, 78 | let fileHandle = FileHandle(forWritingAtPath:logFilePath), 79 | let data = log.data(using:String.Encoding.utf8) { 80 | _ = fileHandle.seekToEndOfFile() 81 | fileHandle.write(data) 82 | fileHandle.closeFile() 83 | } 84 | } 85 | } 86 | 87 | func stringForLogLevel(logLevel:SLogLevel) -> String { 88 | Rainbow.outputTarget = .Console 89 | switch logLevel { 90 | case .Verbose: 91 | return "VERBOSE".green 92 | case .Info: 93 | return "INFO ".white 94 | case .Warning: 95 | return "WARNING".yellow 96 | case .Error: 97 | return "ERROR ".red 98 | case .None: 99 | return "NONE" 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /logexample/Package.swift: -------------------------------------------------------------------------------- 1 | import PackageDescription 2 | 3 | let package = Package( 4 | name:"logexample", 5 | dependencies:[ 6 | .Package(url:"../", majorVersion:1) 7 | ] 8 | ) 9 | -------------------------------------------------------------------------------- /logexample/main.swift: -------------------------------------------------------------------------------- 1 | import swiftlog 2 | 3 | func multiply(_ multiplicand:Int, multiplier:Int) -> Int { 4 | ENTRY_LOG() 5 | let result = multiplicand * multiplier 6 | EXIT_LOG() 7 | return result 8 | } 9 | 10 | slogLevel = .Verbose 11 | slogToFile(atPath:"/tmp/log.txt", append:false) 12 | 13 | SLogVerbose("A verbose log") 14 | SLogInfo("An info log") 15 | SLogWarning("A warning log") 16 | SLogError("An error log") 17 | 18 | SLogVerbose("10 times 10 equals \(multiply(10, multiplier:10))") 19 | --------------------------------------------------------------------------------