Vlang tutorials — logs

Kevin Da Silva
3 min readOct 24, 2023

--

Hi guys It’s been a looooong time since my last blog post, I decided to make my comeback with one more tutorial on how to use a feature in Vlang, today’s feature is a feature I noticed a few weeks ago and immediately applied to one of my projects, you probably already guessed we are talking about the log module.

The log module is a module to add logs to your application, and I will showcase how to use it by adding logs into a url shortener app that I wrote in V.

The first log I added was to print in the terminal when the server has started:
shortener.v


module main

import vweb
import server
import log

fn main() {
log.info('server running')
vweb.run_at(server.new_shortener(), vweb.RunParams{
port: 8081
}) or { panic(err) }
}

To make it work I just imported the module log:

import log

And called the info function with the info message that the server has started:

log.info('server running')

After informing the server status I started to add logs in controllers and services:
controllers.v

module controllers

import structs as s
import requests as r
import httperrors as he
import services
import log

pub fn create_link(req_body r.CreateLinkRequest) s.ResponseObject<s.Response> {
log.info('[CONTROLLER] - create_link function')
if req_body.is_empty() {
log.warn('request without body')
return he.create_response_error(
'fields link_name(string) or link_url(string) should not be empty',
400)
}

return services.create_link(req_body)
}

pub fn get_link(link_name string) s.ResponseObject<s.Response> {
log.info('[CONTROLLER] - get_link function')
return services.get_link_url(link_name)
}

As usual in controllers.v I added two info in both functions create_link, get_link informing that I reached that respective function in the controllers file.

But notice that in the create_link I added a warning if the request body is empty, because if it is It’s not an error on the server, but on the client, so it’s a warning.

Soon we will see how to deal with errors that occur in the server, but first, let´s take a look at the services file:
services.v

module services

import structs as s
import requests as r
import repository as repo
import httperrors as he
import time
import log

pub fn create_link(req_body r.CreateLinkRequest) s.ResponseObject<s.Response> {
log.info('[SERVICE] - create_link function')
db := repo.setup()
if !db.save(req_body.link_name, req_body.link_url) {
log.error('lost connection with database')
return he.create_response_error('unable to reach database', 500)
}

created_link := s.Response(s.CreatedLink{
link_name: req_body.link_name
creation_date: time.now().hhmmss()
})

return s.ResponseObject<s.Response>{
code: 201
body: created_link
format: s.FileFormats.json
}
}

pub fn get_link_url(link_name string) s.ResponseObject<s.Response> {
log.info('[SERVICE] - get_link_url function')
db := repo.setup()
result := db.get(link_name)
if !result.success {
log.warn('${link_name} not found')
return he.create_response_error('url not found', 404)
}

page_link := result.data
script := '<script>window.location.href = "${page_link}"</script>'
response := s.Response(script)

return s.ResponseObject<s.Response>{
code: 200
body: response
format: s.FileFormats.html
}
}

In services, I also added infos informing that we reached the respective functions in the services module, and also added a warning in the get_link_url function when a link URL is not found, but if we take a look at the create_link function we can notice that in this case an error log is emitted because if we can’t communicate with Redis to save a link it means that is a server error, and to inform a server error I used:

log.error('lost connection with database')

I found logs a useful tool to have in the standard library that worked very well with my web server, in case you want to learn more about it I recommend reading the documentation about it here!

Hope you liked it, see ya ;)

--

--

Kevin Da Silva
Kevin Da Silva

Written by Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world

No responses yet