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 AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay