Logging in Objective-C

Here’s the translation of the Go logging example to Objective-C, formatted in Markdown suitable for Hugo:

Our example demonstrates various logging techniques in Objective-C. We’ll use the built-in NSLog function and the CocoaLumberjack framework for more advanced logging capabilities.

First, let’s start with a basic implementation:

#import <Foundation/Foundation.h>
#import <CocoaLumberjack/CocoaLumberjack.h>

static const DDLogLevel ddLogLevel = DDLogLevelVerbose;

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // Basic logging with NSLog
        NSLog(@"standard logger");

        // Configure CocoaLumberjack
        [DDLog addLogger:[DDOSLogger sharedInstance]];

        // Logging with different log levels
        DDLogError(@"This is an error message");
        DDLogWarn(@"This is a warning message");
        DDLogInfo(@"This is an info message");
        DDLogDebug(@"This is a debug message");
        DDLogVerbose(@"This is a verbose message");

        // Logging with a custom logger
        DDFileLogger *fileLogger = [[DDFileLogger alloc] init];
        fileLogger.rollingFrequency = 60 * 60 * 24; // 24 hour rolling
        fileLogger.logFileManager.maximumNumberOfLogFiles = 7;
        [DDLog addLogger:fileLogger];

        DDLogInfo(@"This message will be logged to a file");

        // Logging with additional context
        NSDictionary *context = @{@"key": @"val", @"age": @25};
        DDLogInfo(@"hello again %@", context);
    }
    return 0;
}

In this example, we’re using both NSLog for basic logging and the CocoaLumberjack framework for more advanced logging features. Here’s a breakdown of what’s happening:

  1. We start with a basic NSLog statement, which is similar to the standard logger in Go.

  2. We configure CocoaLumberjack to use the DDOSLogger, which logs to the console.

  3. We demonstrate logging at different levels (error, warning, info, debug, verbose) using CocoaLumberjack.

  4. We create a custom file logger that rotates log files daily and keeps up to 7 log files.

  5. Finally, we show how to log with additional context by passing a dictionary.

To run this program, you’ll need to have the CocoaLumberjack framework installed. You can install it using CocoaPods or Carthage.

Sample output (the date and time will depend on when the example is run):

2023-08-22 10:45:16.000 MyApp[12345:67890] standard logger
2023-08-22 10:45:16.001 MyApp[12345:67890] This is an error message
2023-08-22 10:45:16.002 MyApp[12345:67890] This is a warning message
2023-08-22 10:45:16.003 MyApp[12345:67890] This is an info message
2023-08-22 10:45:16.004 MyApp[12345:67890] This is a debug message
2023-08-22 10:45:16.005 MyApp[12345:67890] This is a verbose message
2023-08-22 10:45:16.006 MyApp[12345:67890] This message will be logged to a file
2023-08-22 10:45:16.007 MyApp[12345:67890] hello again {
    age = 25;
    key = val;
}

Note that Objective-C doesn’t have built-in support for structured logging like Go’s slog package. However, you can achieve similar functionality by using third-party libraries or by formatting your log messages to include structured data.