package little_logger

  1. Overview
  2. Docs
A tiny, little logger <3

Install

Dune Dependency

Authors

Maintainers

Sources

0.1.1.tar.gz
md5=d7fefd0f9925bc1f9106e51b6656f262
sha512=015facc5423acf19ed347e84ca2472c877595df896b387e97a0af04e5b5ff4e14e7840e8f0cc56df3b31b1c86cea7cd3917841bcf2fd0b93c9dfcc5e22d1b939

doc/little_logger/Little_logger/Logger/index.html

Module Little_logger.LoggerSource

A tiny, little logger <3

Overview

Set up

The default log level is Level.Warning and the default printer is prerr_endline.

To change these, use the set_log_level and set_printer functions.

Messages

Logging functions come in two flavors: those that accept a message (e.g., info, error), and those that accept strings directly (e.g., sinfo, serror).

A message is simply a unit -> string thunk. Because message is a thunk, its evaluation won't happen unless the message actually needs to be printed. The type message is just a name given to the thunk to clarify the function signatures.

The functions that take a string directly are prefixed with an s (for string).

Printers

A printer is a function with the following signature string -> unit. The name printer is simply used for clarity.

Common choices for a printer would be prerr_endline to print to stderr, or Async.prerr_endline to asynchronously print to stderr using Async.Writer (e.g., if you are using Jane Street's Async library.

Message Levels

Message levels are hierarchical: given the log level threshold (get_log_level), all messages of equal or higher priority will be logged.

In other words, if a message level is greater than or equal to the logging threshold, it will be printed.

For example, Level.Trace would print all messages, Level.Silent would print no messages, Level.Error would print Level.Error, Level.Fatal, and Level.Unknown messages.

The default log level is Level.Warning. All messages of equal or higher priority than this will be logged.

Examples

Note: the examples assume you have open Little_logger at the top of your file.

Logging messages

  let () = Logger.error (fun () -> "This is an error")
  let () = Logger.serror "This is an error, but using `serror` instead"

  let () =
    Logger.info (fun () ->
        sprintf "I can use %s strings like %s" "format" "this")

Using the thunk accepting functions let you avoid potentially expensive calls in cases where the log level would prevent a message from being printed.

  let () = Logger.set_log_level Logger.Level.Error

  (* This won't run and the expensive thing won't take up extra time. *) let
  () = Logger.debug (fun () -> (* ...Call some expensive log message
  generating function here... *) )

Changing the logging level

The default level is Warning. Here we lower it to Debug.

 let () = Logger.set_log_level Logger.Level.Debug 

Changing the printer

The default printer is prerr_endline, which prints to stderr. We can change to printing to stdout like this.

 let () = Logger.set_printer print_endline 

If you're using the Async library, just change to an async printer.

 let () = Logger.set_printer Async.prerr_endline 

Writing directly to a file

If you want to get crazy you could do something like this to log directly to a file, but I don't know if it is the best idea :)

  let log_fname = "silly_file.txt"
  let log_chan = Out_channel.create "silly_file.txt"

  let printer msg = Out_channel.output_string log_chan (msg ^ "\n")
  let () = Logger.set_printer printer

  let () = Logger.info (fun () -> "Hi file!")

  let () = Out_channel.close log_chan

API

Level

Sourcemodule Level : sig ... end

Logging levels

Type aliases

Sourcetype printer = string -> unit

Type alias for printer functions

Sourcetype message = unit -> string

Type alias for message thunks

Getters and setters

Sourceval get_log_level : unit -> Level.t
Sourceval set_log_level : Level.t -> unit
Sourceval set_printer : printer -> unit

Message accepting log functions

Sourceval unknown : message -> unit

unknown msg logs an unknown message. Unknown messages are printed when log level is Level.Unknown or below.

Sourceval fatal : message -> unit

fatal msg logs an fatal message. Fatal messages are printed when log level is Level.Fatal or below.

Sourceval error : message -> unit

error msg logs an error message. Error messages are printed when log level is Level.Error or below.

Sourceval warning : message -> unit

warning msg logs an warning message. Warning messages are printed when log level is Level.Warning or below.

Sourceval info : message -> unit

info msg logs an info message. Info messages are printed when log level is Level.Info or below.

Sourceval debug : message -> unit

debug msg logs an debug message. Debug messages are printed when log level is Level.Debug or below.

Sourceval trace : message -> unit

trace msg logs an trace message. Trace messages are printed when log level is Level.Trace or below.

String accepting log functions

These are analogous to the message accepting log functions.

Sourceval sunknown : string -> unit
Sourceval sfatal : string -> unit
Sourceval serror : string -> unit
Sourceval swarning : string -> unit
Sourceval sinfo : string -> unit
Sourceval sdebug : string -> unit
Sourceval strace : string -> unit
OCaml

Innovation. Community. Security.