Logs
Where the logs of a Tifa app are and how to write them
Tifa writes its own messages with log/slog, and your code uses the same logger. One call to slog.SetDefault in OnStart changes where all of it goes, in what format and from which level. Tifa does not log each request and has no log level setting of its own. The request log below is about twenty lines of your own.
What Tifa logs
Every message starts with tifa:. The fields are the slog attributes of the line.
ERRORtifa: request failed, withmethod,pathanderr: a route returned an error and the app has noOnError. With anOnError, Tifa logs nothing for it. Report the error there (see Hooks).ERRORtifa: request failed after the response was written, withmethod,pathanderr: the error came too late to change the answer. This one is logged with or withoutOnError.ERRORlines for a page that could not be rendered for anyone (a render failed and no request waits for it,the page with the error failed too), for a session that could not be written to the answer, and for a mistake intifa.SetSitemap(SetSitemap: <kind>).WARNtifa: refused a cross-site write, withmethod,path,reason,origin,sec_fetch_siteandhost. See Cross-site writes.WARNtifa: dropping a session, withwhy,methodandpath: the stored session of a request could not be used (it expired, is damaged or does not fit the type that reads it), so the visitor is signed out.WARNonce, when a sign-in comes over plain HTTP and the browser will drop the Secure cookie.WARNandERRORlines from the render (tifa: ssr:):WARNfor a call toIntlwithout a locale (see Warnings intifa dev) and for a VM that was replaced, with the reason.ERRORwhen the garbage collection of a VM failed, or a VM could not be replaced.WARNandINFOlines about HTTPS: the certificate that was obtained or loaded (domain,valid_until), a certificate that is not there yet, and HTTPS on a loopback address.INFOtifa: listening on <address>. It comes afterOnStartreturns, so a handler set there gets it. Undertifa devthe app does not print it.INFOone-time notes: sessions are kept in memory,sitemap.xmllists only pages without parameters, and a page ofcontent/was published.
Lines about a request use the Context forms, so a handler that reads the context of the request sees it.
Logging from your code
Use log/slog and pass the context of the request, so that a handler that reads it, for a request ID or a trace, gets it:
func POST(c *tifa.Ctx) error {
// ...
slog.InfoContext(c.Context(), "order placed", "order", id, "total", total)
return c.JSON(201, order)
}
slog.WarnContext, slog.ErrorContext and slog.DebugContext work the same. Outside a request, such as in OnStart or a job, use slog.Info and the others.
Format and level
slog.SetDefault in OnStart sets the handler for Tifa and for your code. The level is a setting of the handler. Here it comes from an option of the app (see App options), so LOG_LEVEL=debug or --log-level debug changes it without a build.
options.go:
var LogLevel = tifa.String("log.level", "info", "Log level: debug, info, warn or error")
hooks.go:
func OnStart(ctx context.Context) error {
var level slog.Level
if err := level.UnmarshalText([]byte(LogLevel)); err != nil {
return fmt.Errorf("log.level: %w", err)
}
opts := &slog.HandlerOptions{Level: level}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, opts)))
return nil
}
slog.NewTextHandler writes key=value lines for reading in a terminal. Without SetDefault, the log goes to stderr as text, at level INFO and above. To change the level while the program runs, keep a slog.LevelVar and pass &slog.HandlerOptions{Level: &levelVar}, then call levelVar.Set(...) from your own code.
Logging each request
Tifa has no request log. Wrap the app with tifa.SetHTTPHandler in OnStart (see Hooks). The wrapper times the request and reads the route template from r.Pattern once the app has answered:
type statusWriter struct {
http.ResponseWriter
status int
}
func (w *statusWriter) WriteHeader(code int) {
w.status = code
w.ResponseWriter.WriteHeader(code)
}
// Unwrap lets http.ResponseController reach Flush, SetWriteDeadline and Hijack below.
func (w *statusWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter }
func logRequests(app http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/assets/") {
app.ServeHTTP(w, r) // bundle files: no line
return
}
start := time.Now()
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
app.ServeHTTP(sw, r)
slog.InfoContext(r.Context(), "request",
"method", r.Method, "route", r.Pattern, "status", sw.status, "duration", time.Since(start))
})
}
Then in OnStart: tifa.SetHTTPHandler(logRequests).
routeis the template, such as/projects/{projectId}, not the URL, so lines of one page group together and carry no ids. It is empty when no route matched: an unmatched URL (404) and a file of thepublicfolder. Addr.URL.Pathto the line if you want the path.- A client navigation asks for its data with the
_tifaquery parameter, and.pmarks a preload. Each is a request with a line of its own, so the number of lines is not the number of page views (see Hooks). - Files under
/assets/are the bundle of the browser, and the example skips them. Files of thepublicfolder have no prefix to tell them apart, so they get a line. - A stream, such as server-sent events, is logged when it ends, with its whole duration (see Streaming). Streaming flushes through
http.ResponseController, which findsFlushby theUnwrapmethod. WithoutUnwrapthe wrapper holds the stream back. - The status is what the app wrote. The wrapper sees the 403 of a refused cross-site write too.
Where the logs go
tifa dev: the terminal. With--background, all output goes to.tifa/dev.log: read it withgo tool tifa dev logs, and follow it with-f(see Logs).- A server set up with
tifa deploy: stdout and stderr go to systemd's journal.go tool tifa deploy logsprints the last 100 lines,-n 500for more and-ffor the new ones. On the server,journalctl --user -u tifa-<app>as the usertifadoes the same (see Logs, status and remove). - How long the server keeps them: that is the journald setup of the server, not Tifa. The limits are in
/etc/systemd/journald.conf, such asSystemMaxUsefor the disk it may use andMaxRetentionSecfor the age.journalctl --disk-usageshows what it uses now. A journal kept in memory (Storage=volatile) is gone after a restart of the server. - A container: stdout and stderr. The runtime of the container, such as Docker or Kubernetes, collects them. Send the log to stdout, as the example of Format and level does, and keep it out of files in the container.