Mar*_*ino 35 logging objective-c ios lumberjack
我在iPhone项目中使用CocoaLumberjack来记录一些信息.
我已经按照入门指南,一切正常,但有一件事让我感到困惑:似乎没有一种优雅的方式来定义整个应用程序的日志级别.为了使它工作,我需要在每个源文件中定义一个常量,如下所示:
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法为应用程序定义全局日志级别?
Fre*_*eer 21
您可以#include
在*.pch文件中使用一个语句,以便它自动包含在您的所有项目文件中.
Mar*_*ino 18
我没有找到比我在问题中提到的文章中解释的更好的方法.
Constant.h
extern int const ddLogLevel;
Run Code Online (Sandbox Code Playgroud)
Constant.m
#import "Constants.h"
#import "DDLog.h"
int const ddLogLevel = LOG_LEVEL_VERBOSE;
Run Code Online (Sandbox Code Playgroud)
记录器配置
#import "DDLog.h"
#import "DDASLLogger.h"
#import "DDTTYLogger.h"
#import "DDFileLogger.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
[DDLog addLogger:fileLogger];
[fileLogger release];
...
Run Code Online (Sandbox Code Playgroud)
导入你的课程
#import "DDLog.h"
#import "Constants.h"
...
- (void)someMethod {
DDLogVerbose(@"Log this message");
}
Run Code Online (Sandbox Code Playgroud)
Cam*_*mer 16
您不需要现已弃用的.pch
文件,只需在需要时包含头文件即可.
#ifndef Project_Logger_h
#define Project_Logger_h
#if defined(__OBJC__)
#import <CocoaLumberjack/DDLog.h>
extern int ddLogLevel;
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
#import "Logger.h"
int ddLogLevel = LOG_LEVEL_VERBOSE;
Run Code Online (Sandbox Code Playgroud)
#import <CocoaLumberjack/CocoaLumberjack.h>
int ddLogLevel = DDLogLevelVerbose;
Run Code Online (Sandbox Code Playgroud)
如果2.0超出测试版时语法发生变化,请评论或编辑.
#import "AppDelegate.h"
#import "Logger.h"
#import <CocoaLumberjack/DDFileLogger.h>
#import <CocoaLumberjack/DDASLLogger.h>
#import <CocoaLumberjack/DDTTYLogger.h>
@interface AppDelegate ()
@property (strong, nonatomic) DDFileLogger *fileLogger;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
[DDLog addLogger:fileLogger];
self.fileLogger = fileLogger;
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
}
- (void)applicationWillTerminate:(UIApplication *)application
{
DDLogDebug(@"%s", __PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)
您可以在*.pch文件中使用它来自动获取不同的全局日志级别,具体取决于您当前的构建配置.[for xcode 4+]
#ifdef DEBUG
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
#else
static const int ddLogLevel = LOG_LEVEL_WARN;
#endif
Run Code Online (Sandbox Code Playgroud)
或者如果每个记录器需要不同的日志级别,则可以使用DDLog + addLogger:withLogLevel:方法轻松实现此目的.
[DDLog addLogger:[DDASLLogger sharedInstance] withLogLevel:LOG_LEVEL_INFO];
[DDLog addLogger:[DDTTYLogger sharedInstance] withLogLevel:LOG_LEVEL_DEBUG];
Run Code Online (Sandbox Code Playgroud)
在您提到的每个源文件中定义日志级别都有好处.您可以仅为当前正在处理的部分使用详细日志记录级别.对于休息部分,您可以使用其他级别,如信息,警告,错误.
为了动态注入日志级别(例如,从配置文件):
1) 使用以下代码创建一个名为 DDLogLevel 的新类:
#import "DDLogLevel.h"
#import "DDLog.h"
@implementation DDLogLevel
static int _ddLogLevel = LOG_LEVEL_VERBOSE;
+ (int)ddLogLevel
{
return _ddLogLevel;
}
+ (void)ddSetLogLevel:(int)logLevel
{
_ddLogLevel = logLevel;
}
@end
Run Code Online (Sandbox Code Playgroud)
2) 在 DDLogLevel.h 中,找到包含以下语句的行:
#ifndef LOG_LEVEL_DEF
#define LOG_LEVEL_DEF ddLogLevel
#endif
Run Code Online (Sandbox Code Playgroud)
并将其替换为:
#ifndef LOG_LEVEL_DEF
#define LOG_LEVEL_DEF [DDLogLevel ddLogLevel]
#endif
Run Code Online (Sandbox Code Playgroud)
3) 最后,从您的初始化过程(可能从 appDelegate)调用具有所需级别的 ddSetLogLevel。
对于使用 CocoaLumberjackSwift 的用户,您可以简单地在代码中的任意位置设置以下全局变量:
dynamicLogLevel = .verbose
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15299 次 |
最近记录: |