Does anyone have a swift example on how to use multiple file loggers. At times, I only want to log to one file and then to a different file. There's a stack overflow posting here (http://stackoverflow.com/questions/12921740/cocoalumberjack-filelogger-logging-to-multiple-files) but it's in objective C and they don't show how to write to different files. There's a subtle example but it's not clear.
Did you check here?
was able to figure this out....delete if possible
It is better to leave it here for other users that may have the same question.
It would be even better if you shared your solution here too ;)
I'm pretty sure, everyone else would figure it out faster than me. Also, I realized I was making it harder than it needed to be. I was trying to have a separate file for warnings, info, errors, etc. But I don't need that really, so I am using it the way the documentation suggest. Although I doubt it will help anyone, here is a swift snippet. (I am using AFDateHelper by the way)
(edited. adding an example of using formatters to write logs to different files)
//In some constants file
let kdetailedLogsDirectory = "detailedLogs"
let kinformationalLogsOnlyDirectory = “informationalLogs"
class CustomLoggingFormatter : NSObject, DDLogFormatter{
//Not going to worry about thread safety for NSDateFormatter for now
func formatLogMessage(logMessage: DDLogMessage!) -> String! {
var logLevel : String!
switch logMessage.flag { //You can create your own logging level but not necessary here
case DDLogFlag.Info: logLevel = "[INFORMATIONAL] :" ; break;
case DDLogFlag.Debug: logLevel = "[DEBUG] :"; break;
case DDLogFlag.Warning: logLevel = "[WARNING] :"; break;
case DDLogFlag.Error: logLevel = "[ERROR] :";break;
default : logLevel = "[VERBOSE] :";break;
}
return "(\(NSDate().toString(format: DateFormat.Custom("yyyy/MM/dd HH:mm:ss:SSS")))) \(logLevel) \(logMessage.message)"
}
}
class CustomLoggingFormatterOnlyForInformational : NSObject, DDLogFormatter{
//Not going to worry about thread safety for NSDateFormatter for now
//You only want to log informational data. Return nil if the flag isn't the .Info level
func formatLogMessage(logMessage: DDLogMessage!) -> String! {
if(logMessage.flag == DDLogFlag.Info){
return "(\(NSDate().toString(format: DateFormat.Custom("yyyy/MM/dd HH:mm:ss:SSS")))) [INFORMATIONAL] : \(logMessage.message)"
}
return nil
}
}
//Set up the logging.
let logsDirectoryURL = getDocumentsDirectoryURL()?.URLByAppendingPathComponent(kdetailedLogsDirectory)
let logFileManager = DDLogFileManagerDefault(logsDirectory: logsDirectoryURL!.path!)
let logFile = DDFileLogger(logFileManager: logFileManager)
logFile.logFormatter = CustomLoggingFormatter()
//Set up a log file that only captures informational logging.
let onlyInfologsDirectoryURL =
getDocumentsDirectoryURL()?.URLByAppendingPathComponent(kinformationalLogsOnlyDirectory)
let onlyInfologFileManager = DDLogFileManagerDefault(logsDirectory: onlyInfologsDirectoryURL!.path!)
let onlyInfologFile = DDFileLogger(logFileManager: onlyInfologFileManager)
onlyInfologFile.logFormatter = CustomLoggingFormatterOnlyForInformational()
DDLog.addLogger(logFile)
DDLog.addLogger(onlyInfologFile)
if this helps one person, I'll be thrilled
The docs say: You'll also notice there is a context method/ivar. Now, by default, the log context of every message is zero. However, this can easily be customized.
If anyone knows how to do this is swift, an example would be appreciated. Thanks
I figured out how to use multiple files and using formatters to log to specific files if necessary.
Thanks for the contribution!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.
Most helpful comment
I'm pretty sure, everyone else would figure it out faster than me. Also, I realized I was making it harder than it needed to be. I was trying to have a separate file for warnings, info, errors, etc. But I don't need that really, so I am using it the way the documentation suggest. Although I doubt it will help anyone, here is a swift snippet. (I am using AFDateHelper by the way)
(edited. adding an example of using formatters to write logs to different files)
if this helps one person, I'll be thrilled