LoginSignup
11

More than 5 years have passed since last update.

ログ表示ライブラリYosakuの紹介

Last updated at Posted at 2014-08-28

Yosakuとは

iOSアプリ開発において、アプリケーションの出力するログをデバイス上で確認できたほうが良い場合があります。 YosakuCocoaLumberjack のロガーとして、UITableView上にログレコードを表示するライブラリです。

Yosaku on iPhone portrait

Yosaku on iPhone landspace

使い方

Yosakuは CocoaPods から導入できます。この時依存するCocoaLumberjackも導入されます。

Podfile
pods 'yosaku', '~> 0.2.0' # for CocoaLumberjack 2.0.x
Podfile
pods 'yosaku', '~> 0.1.1' # for CocoaLumberjack 1.9.x

YosakuはYSLoggerというDDLoggerの実装を提供しています。これを初期化ののち、CocoaLumberjackの他のロガー同様にDDLogに追加します。

(AppDelegateでの初期化例)
#import <YSLogger.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"JST"];
    dateFormatter.dateFormat = @"yyyy/MM/dd HH:mm:ss.SSS";

    _logger = [[YSLogger alloc] initWithCapacity:100 dateFormatter:dateFormatter];
    _logger.updateIntervalSec = 1.5;
    [DDLog addLogger:_logger];

    return YES;
}

なお、YSLoggerの初期化時にログレコードの時刻フォーマッタ、ログレコード保持数、ログレコード表示更新インターバル(秒)を与えます。

ログレコードの表示には表示用の空のUITableViewを用意し、YSLoggerにセットします。YSLoggerUITableViewDataSource, UITableViewDelegateとして自身をテーブルビューにセットします。

UIViewController
- (void)viewDidLoad
{
    [super viewDidLoad];

    // _app.logger is a YSLogger
    _app = [UIApplication sharedApplication].delegate;
    _app.logger.tableView = _logTableView; // YSLogger set myself to tableView's data source and delegate
    [_app.logger viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_app.logger viewWillDisappear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [_app.logger viewDidAppear:animated];
}

ログレコードの表示ビューが非表示になった場合は画面更新処理が不要になるため、UIViewControllerviewDidAppearviewWillDisappearで同名のロガーのメソッドを呼ぶようにしてください。

追記

  • Yosaku 0.2.0, 0.1.1 をリリースしました。0.2.0 は CocoaLumberjack 2.0.0 に対応しています。CocoaLumberjack 1.9.x をお使いの方は Yosaku 0.1.1 をお使いください。 (2015年3月24日)

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
11