以下は、DanSkeels DSLogging コードを使用した動的ログの例です。
GFDPerson.h
#import <Foundation/Foundation.h>
@interface GFDPerson : NSObject{
@protected
NSArray *pLogLevelNames;
NSArray *pLogLevelKeys;
NSDictionary *pLogLevels;
}
-(void)logPerson;
-(void)setLogLevel:(NSUInteger)logLevel;
@end
GFDPerson.m
#import "GFDPerson.h"
#import "DSLogging.h"
DSLogLevelSetupMutable(DDLogLevelWarning);
@implementation GFDPerson
-(id)init{
if (self = [super init]) {
pLogLevelNames = [[NSArray alloc] initWithObjects:
@"no logging",
@"only errors",
@"errors and warnings",
@"errors, warnings and infos",
@"verbose",
nil];
pLogLevelKeys = [[NSArray alloc] initWithObjects:
[[NSNumber numberWithInteger:DDLogLevelOff]stringValue],
[[NSNumber numberWithInteger:DDLogLevelError]stringValue],
[[NSNumber numberWithInteger:DDLogLevelWarning]stringValue],
[[NSNumber numberWithInteger:DDLogLevelInfo]stringValue],
[[NSNumber numberWithInteger:DDLogLevelVerbose]stringValue],
nil];
pLogLevels = [[NSDictionary alloc]initWithObjects:pLogLevelNames
forKeys:pLogLevelKeys];
return self;
}
return nil;
}
-(void)setLogLevel:(NSUInteger)logLevel{
ddLogLevel = logLevel;
}
-(void)logPerson{
NSLog(@"Person is logging with Loglevel: %@",[pLogLevels valueForKey: [[NSNumber numberWithInteger:ddLogLevel]stringValue]]);
DDLogVerbose(@"Person log verbose");
DDLogInfo(@"Person log info");
DDLogWarn(@"Person log warning");
DDLogError(@"Person log error");
DDLogDebug(@"Person log debug");
}
@end
main.m
#import <Foundation/Foundation.h>
#import "DSLogging.h"
#import "GFDPerson.h"
DSLogLevelSetupMutable(DDLogLevelError);
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
[DDLog addLogger:[DDASLLogger sharedInstance]];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
ddLogLevel = DDLogLevelWarning;
NSLog(@"Warning:");
DDLogWarn(@"WARNING LOG!");
DDLogError(@"ERROR LOG!");
DDLogVerbose(@"VERBOSE LOG!");
ddLogLevel = DDLogLevelError;
NSLog(@"Error:");
DDLogWarn(@"WARNING LOG!");
DDLogError(@"ERROR LOG!");
DDLogVerbose(@"VERBOSE LOG!");
ddLogLevel = DDLogLevelOff;
NSLog(@"Off:");
DDLogWarn(@"WARNING LOG!");
DDLogError(@"ERROR LOG!");
DDLogVerbose(@"VERBOSE LOG!");
ddLogLevel = DDLogLevelVerbose;
NSLog(@"Verbose:");
DDLogWarn(@"WARNING LOG!");
DDLogError(@"ERROR LOG!");
DDLogVerbose(@"VERBOSE LOG!");
ddLogLevel = DDLogLevelOff;
GFDPerson *personA = [[GFDPerson alloc] init];
[personA logPerson];
[personA setLogLevel:DDLogLevelVerbose];
[personA logPerson];
[personA setLogLevel:DDLogLevelError];
[personA logPerson];
}
return 0;
}
このコードの出力:
Warning:
WARNING LOG!
ERROR LOG!
Error:
ERROR LOG!
Off:
Verbose:
WARNING LOG!
ERROR LOG!
VERBOSE LOG!
Person is logging with Loglevel: errors and warnings
Person log warning
Person log error
Person is logging with Loglevel: verbose
Person log verbose
Person log info
Person log warning
Person log error
Person log debug
Person is logging with Loglevel: only errors
Person log error
コメントしてください、私が何かを誤解したり誤用したりした場合...