Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Application Logs to File and get them

I´m taking baby steps in iOS development and searching for a method to use logging in iOS.

I found these docs about logging with swift 3 : https://developer.apple.com/documentation/os/logging#1682426

The Docs say that the logs aren´t saved on disk. What is the typical way to get the logs and process the file?

like image 589
Offset Avatar asked Jun 14 '17 06:06

Offset


People also ask

How can you get log files for a application?

On a Windows computer: Inside the Control Panel, find System & Security. From there, go to Administrative Tools and then the Event Viewer. Open Windows Logs and choose Application. This will show you all the application logs saved on your computer.

How do I get to the log file?

Because most log files are recorded in plain text, the use of any text editor will do just fine to open it. By default, Windows will use Notepad to open a LOG file when you double-click on it. You almost certainly have an app already built-in or installed on your system for opening LOG files.

Where application logs are stored?

WebSphere Application Server application log files can be found in the logs folder for each profile in the cell; for example, $WAS_HOME/profiles/<profile_name>/logs. There is a folder for each server in the logs directory. This location is where the JVM stdout and stderr logs (SystemOut.


1 Answers

put this file to your project

//
//  log.swift
//  logtest
//

import Foundation

struct Log: TextOutputStream {

    func write(_ string: String) {
        let fm = FileManager.default
        let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
        if let handle = try? FileHandle(forWritingTo: log) {
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } else {
            try? string.data(using: .utf8)?.write(to: log)
        }
    }
}

var logger = Log()

and if you need something to be logged, just use print function like

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        print("started:", Date(), to: &logger)
        return true
    }

or

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    print(#file, #function, "my own text", 1, [1,2], to: &logger)
}

in your application's 'Documents' folder you could find 'log.txt' file which you could examine later.

while running my test application twice, the content looks like

started: 2017-06-14 09:58:58 +0000
/Users/ivo_vacek/Documents/logtest/logtest/ViewController.swift viewDidLoad() my own text 1 [1, 2]
started: 2017-06-14 09:59:15 +0000
/Users/ivo_vacek/Documents/logtest/logtest/ViewController.swift viewDidLoad() my own text 1 [1, 2] 

if you don't like 'globals' define Log as singletone class

class Log: TextOutputStream {

    func write(_ string: String) {
        let fm = FileManager.default
        let log = fm.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("log.txt")
        if let handle = try? FileHandle(forWritingTo: log) {
            handle.seekToEndOfFile()
            handle.write(string.data(using: .utf8)!)
            handle.closeFile()
        } else {
            try? string.data(using: .utf8)?.write(to: log)
        }
    }
    static var log: Log = Log()
    private init() {} // we are sure, nobody else could create it
}

and use it like

print("started:", Date(), to: &Log.log)
like image 134
user3441734 Avatar answered Oct 20 '22 17:10

user3441734