LoginSignup
22
19

More than 5 years have passed since last update.

Swiftの自前ログクラス

Posted at

Swiftを少し触り始めたので、その時のメモを残します。

はじめに

自分仕様にログの出し方を整えておきたいと思い、ログクラスを作ることにしました。
Swiftもそうですが、Objective-Cもあまり触ったことがないので、もしかしたら、もっと簡単なやり方があるのかもしれませんが。

ログ出力でやりたいこと

  • 時間を出力したい
  • 実行行を出力したい
  • 実行クラス、メソッドを出力したい
  • ログレベルを出力したい

とりあえずこんなところでしょうか。

Logger.swift

Logger.swift
import Foundation

class Logger{
    class func debug(
            message: String,
            function: String = __FUNCTION__,
            file: String = __FILE__,
            line: Int = __LINE__) { Logger.write("[DEBUG]", message: message, function: function, file: file, line: line) };
    class func info(
            message: String,
            function: String = __FUNCTION__,
            file: String = __FILE__,
            line: Int = __LINE__) { Logger.write("[INFO]", message: message, function: function, file: file, line: line) };
    class func warning(
            message: String,
            function: String = __FUNCTION__,
            file: String = __FILE__,
            line: Int = __LINE__) { Logger.write("[WARNING]", message: message, function: function, file: file, line: line) };
    class func error(
            message: String,
            function: String = __FUNCTION__,
            file: String = __FILE__,
            line: Int = __LINE__) { Logger.write("[ERROR]", message: message, function: function, file: file, line: line) };
    class func write(
            loglevel: String,
            message: String,
            function: String,
            file: String,
            line: Int) {

            let now = NSDate() // 現在日時の取得
            let dateFormatter = NSDateFormatter()
            dateFormatter.locale = NSLocale(localeIdentifier: "ja_JP")
            dateFormatter.timeStyle = .MediumStyle
            dateFormatter.dateStyle = .MediumStyle
            //println(dateFormatter.stringFromDate(now)) // => 2014/12/11 15:19:04

            var nowdate = dateFormatter.stringFromDate(now)

            var filename = file
            if let match = filename.rangeOfString("[^/]*$", options: .RegularExpressionSearch) {
                filename = filename.substringWithRange(match)
            }
            println("\(loglevel) => \"\(message)\" \(nowdate) L\(line) \(function) @\(filename)")
    }
}

まあ、なんというかもしかしたらもっと良いやり方があるんかもしれないのですが。

使い方

DEBUG,INFO,WARNING,ERROR

Logger.debug("hogehoge")
Logger.info("hogehoge")
Logger.warning("hogehoge")
Logger.error("hogehoge")

出力結果

[DEBUG] => "hogehoge" 2014/12/11 15:56:16 L21 application(_:didFinishLaunchingWithOptions:) @AppDelegate.swift

[INFO] => "hogehoge" 2014/12/11 15:56:16 L22 application(_:didFinishLaunchingWithOptions:) @AppDelegate.swift

[WARNING] => "hogehoge" 2014/12/11 15:56:16 L23 application(_:didFinishLaunchingWithOptions:) @AppDelegate.swift

[ERROR] => "hogehoge" 2014/12/11 15:56:16 L24 application(_:didFinishLaunchingWithOptions:) @AppDelegate.swift

22
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
19