Currently in Beta - View old docs

HeliumLogger

Helium Logger is a lightweight Swift logging framework which supports logging to standard output.

If you created your server using `kitura init` or the macOS app you can skip to step 3.

Prerequisites

Step 1: Add HeliumLogger to project

To start we need to import LoggerAPI in our code:

import LoggerAPI

LoggerAPI is a logger protocol that provides a common logging interface for different kinds of loggers.

Kitura has LoggerAPI included as a dependency by default, so we do not need to add this to our `Package.swift`

Now we need to import HeliumLogger:

import HeliumLogger

Step 2: Add logging code to the server

To use the default settings for HeliumLogger we just need to call the `use()` method in HeliumLogger.

Inside `Sources` > `MyKituraApp` > `main.swift` add:

HeliumLogger.use()

We are going to use our new logger to for errors thrown by our server.

Inside the catch block, log the thrown error:


    Log.error(error.localizedDescription)
    

Your `main.swift` should look like this:

import Foundation
    import Kitura
    import LoggerAPI
    import HeliumLogger
    import Application

    HeliumLogger.use()
    do {
        let app = try App()
        try app.run()
    } catch let error {
        Log.error(error.localizedDescription)
    }

Now if you start your server you should see some output:


    [xxx] [VERBOSE] [Kitura.swift:118 run()] Starting Kitura framework...
    [xxx] [VERBOSE] [Kitura.swift:135 start()] Starting an HTTP Server on port 8080...
    [xxx] [INFO] [HTTPServer.swift:195 listen(on:)] Listening on port 8080
    [xxx] [VERBOSE] [HTTPServer.swift:196 listen(on:)] Options for port 8080: maxPendingConnections: 100, allowPortReuse: false

Step 3: Set logging level

HeliumLogger can be customized to change the level of logging.

This means you can decide how much information you'd like to be logged, based on importance.

If you'd like to customize the level of logging you can simply pass the level to the `use()` method:

HeliumLogger.use(.error)

There are several logging levels:

  1. entry
  2. exit
  3. debug
  4. verbose (default)
  5. info
  6. warning
  7. error

Verbose is set by default. Setting a particular logging level also includes all levels above it.

For example, if you set a logging level of `verbose`:

HeliumLogger.use(.verbose)

You will get logging for; `verbose`, `info`, `warning` and `error`.

Similarly if you set a logging level of `warning`:

HeliumLogger.use(.warning)

You will get logging for `warning` and `error`.

Step 4: Add logging to your application

Once you have your Logger configured, you can use it within your application.

Open your `Sources` > `Application` > `Application.swift` file and ensure LoggerAPI is imported.

import LoggerAPI

Add a log message to the App's `init()` function:

Log.info("Hello World")

Restart your server and look through the logs. You should see your message:

[2019-03-13T16:23:08.078Z] [INFO] [Application.swift:22 init()] Hello World

Your log message is printed along with the time it occured, the log level and the location in your code.

Next steps

Add routing: Add REST APIs, such as HTTP GET, to your server.

Back to top