DDLogFormatter

Objective-C

@protocol DDLogFormatter <NSObject>

Swift

protocol DDLogFormatter : NSObjectProtocol

This protocol describes the behavior of a log formatter

  • Formatters may optionally be added to any logger. This allows for increased flexibility in the logging environment. For example, log messages for log files may be formatted differently than log messages for the console.

    For more information about formatters, see the “Custom Formatters” page: Documentation/CustomFormatters.md

    The formatter may also optionally filter the log message by returning nil, in which case the logger will not log the message.

    Declaration

    Objective-C

    - (nullable NSString *)formatLogMessage:(nonnull DDLogMessage *)logMessage;

    Swift

    func format(message logMessage: DDLogMessage) -> String?
  • A single formatter instance can be added to multiple loggers. These methods provides hooks to notify the formatter of when it’s added/removed.

    This is primarily for thread-safety. If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers. Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter with 10.0 behavior), it could possibly use these hooks to switch to thread-safe versions of the code.

    Declaration

    Objective-C

    - (void)didAddToLogger:(nonnull id<DDLogger>)logger;

    Swift

    optional func didAdd(to logger: DDLogger)
    • A single formatter instance can be added to multiple loggers.
    • These methods provides hooks to notify the formatter of when it’s added/removed. *
    • This is primarily for thread-safety.
    • If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
    • Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter with 10.0 behavior),
    • it could possibly use these hooks to switch to thread-safe versions of the code or use dispatch_set_specific() .* to add its own specific values. *

    Declaration

    Objective-C

    - (void)didAddToLogger:(nonnull id<DDLogger>)logger
                   inQueue:(nonnull dispatch_queue_t)queue;

    Swift

    optional func didAdd(to logger: DDLogger, in queue: DispatchQueue)
  • See the above description for didAddToLogger:

    Declaration

    Objective-C

    - (void)willRemoveFromLogger:(nonnull id<DDLogger>)logger;

    Swift

    optional func willRemove(from logger: DDLogger)