DEV Community

Cover image for Mantis - new file log driver
Anwar
Anwar

Posted on • Edited 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.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")!

        return app.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.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 app.response.html(
          content: h1({}, ['Oops! Something went wrong'])
          status: .server_error
        )
      }
    }
    routes: [
      route.get(name: "index", path: "/", callback: fn (app App) Response {
        return app.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!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay