您的位置:首页 > 移动开发 > Swift

使用XcodeColors 来显示XCGLogger,进行swift 的logger定制

2016-03-17 00:11 525 查看
XcodeColors项目地址

XcodeColors installation instructions for Xcode 4, 5, 6 & 7:

Download or clone the repository.

Open the XcodeColors project with Xcode

If compiling for Xcode 4, then change the schemes to use the Xcode4 build configuration (instead of the Xcode5 build configuration which is the default)

Compile the XcodeColors target.

When you do this, the Xcode plugin is automatically copied to the proper location.

This is done via the build settings. You can validate the plugin was copied to “~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin”

Now completely Quit Xcode

Re-Launch Xcode, and re-open the XcodeColors project

Now run the TestXcodeColors target.

This will test your installation, and you should see colors in your Xcode console.

Did you upgrade Xcode and now XcodeColors is “broken”? Get the fix here: XcodeUpdates.

$ ./update_compat.sh

XCGLogger中定义log显示内容的颜色

在AppDelegate.swift中定义一个全局变量log

//
//  AppDelegate.swift
//  Meerkat
//
//  Created by 徐泽宇 on 16/3/5.
//  Copyright © 2016年 九象网络科技(上海). All rights reserved.
//

import UIKit
import XCGLogger

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
let log: XCGLogger = {
let log = XCGLogger.defaultInstance()
log.setup(.Debug, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil, fileLogLevel: .Debug)

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd hh:mma"
dateFormatter.locale = NSLocale.currentLocale()
log.dateFormatter = dateFormatter

return log
}()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

//使用XCGLogger记录日志 begin

log.xcodeColorsEnabled = true // Or set the XcodeColors environment variable in your scheme to YES
log.xcodeColors = [
.Verbose: .lightGrey,
.Debug: .darkGrey,
.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
]
//使用XCGLogger记录日志 end

//   启动友盟的 分析功能 begin
MobClick.startWithAppkey("1111111111", reportPolicy: SEND_INTERVAL, channelId: nil)
MobClick.setLogEnabled(true)
//友盟SDK为了兼容Xcode3的工程,默认取的是Xcode的Build号。如果需要取Xcode4及以上版本的Version,可以使用下面 的方法。
let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
MobClick.setAppVersion(version)
//  启动友盟的 分析功能 end
log.debug("友盟统计sdk载入完成!")
return true
}

func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}


在其他的类中引用这个log进行输出

var appDelegate=AppDelegate()
appDelegate.log.debug("摄像头界面初始化完成!")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: