Last active
April 16, 2016 09:19
-
-
Save khanhldt/fb1f9428f49168f7a29ac25b50f6a58c to your computer and use it in GitHub Desktop.
Utility class to use a global XCGLogger from anywhere in the project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Khanh Le Do on 12/3/16. | |
// Copyright © 2016 estasy. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import XCGLogger | |
public class LogUtils { | |
private static let cacheDirectory: NSURL = { | |
let urls = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask) | |
return urls[urls.endIndex - 1] | |
}() | |
public static let logger: XCGLogger = { | |
// Setup XCGLogger | |
let aLogger = XCGLogger.defaultInstance() | |
aLogger.xcodeColorsEnabled = true // Or set the XcodeColors environment variable in your scheme to YES | |
aLogger.xcodeColors = [ | |
.Verbose: .darkGrey, | |
.Debug: .blue, | |
.Info: .darkGreen, | |
.Warning: .orange, | |
.Error: XCGLogger.XcodeColor(fg: UIColor.redColor(), bg: UIColor.whiteColor()), // Optionally use a UIColor | |
.Severe: XCGLogger.XcodeColor(fg: (255, 255, 255), bg: (255, 0, 0)) // Optionally use RGB values directly | |
] | |
// == Configuration | |
// Turn verbose on only for local testing purpose | |
let logLevel = XCGLogger.LogLevel.Debug | |
#if USE_NSLOG // Set via Build Settings, under Other Swift Flags | |
aLogger.setup(logLevel, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true) | |
#else | |
let logPath: NSURL = LogUtils.cacheDirectory.URLByAppendingPathComponent("out.log") | |
aLogger.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: logPath) | |
#endif | |
return aLogger | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment