DEV Community

Cover image for Mantis - new file log driver
Anwar
Anwar

Posted on

Mantis - new file log driver

Mantis is a type-safe web framework written in V that emphasizes explicit, magic-free code.

In this post, we'll explore the new file-based logging system and see how it integrates seamlessly with other Mantis features.

File logging

To log on a file, you just need to configure your http App by specifying the available File log channel.

module main

import khalyomede.mantis.http { create_app, App, Response }
import khalyomede.mantis.http.route
import khalyomede.mantis.http.response
import khalyomede.mantis.logging { Log }
import khalyomede.mantis.logging.channel { File }

fn main() {
  app := create_app(
    log: {
      channel: File{
        path: "logs/mantis.log"
      }
    }
    routes: [
      route.get(path: "/", callback: fn (app App) Response {
        app.log.debug("new user on the home page") or {}

        return response.html(content: "hello world")
      })
    ]
  )

  app.serve() or { panic(err) }
}
Enter fullscreen mode Exit fullscreen mode

Coupled with error handling

Error handling and logging work together to help you track and debug issues in production. Here's how to configure both:

module main

import khalyomede.mantis.http { create_app, App, Response, ErrorHandler } 
import khalyomede.mantis.html { h1, div, p }
import khalyomede.mantis.http.response
import khalyomede.mantis.http.route
import khalyomede.mantis.logging { Log }
import khalyomede.mantis.logging.channel { File }

fn main() {
  app := create_app(
    log: {
      channel: File{
        path: "logs/mantis.log"
      }
    }
    error_handler: ErrorHandler{ 
      report: fn (app App, err IError) {
        app.log.log(.error, err.msg()) or {} // <-----
      }
      render: fn (app App, err IError) Response {
        return response.html(
          content: h1({}, ['Oops! Something went wrong'])
          status: .server_error
        )
      }
    }
    routes: [
      route.get(name: "index", path: "/", callback: fn (app App) Response {
        return response.html(content: div({}, ["Home page"]))
      })
    ]
  )

  app.serve() or { panic(err) }
}
Enter fullscreen mode Exit fullscreen mode

Standalone logging

Logging can be used outside an HTTP App. Read the documentation to learn more about it.

To go further

Learn more about logging, including dynamic severity by reading the documentation.

Let me know on Github issues or on Twitter if you find anything or want to contribute with any ideas.

Happy logging!

Top comments (0)