Ниже приведен пример динамического ведения журнала, в котором используется код DSLogging DanSkeels:
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
Прокомментируйте, пожалуйста, если я что-то неправильно понял или неправильно использовал ...