Lwt_logLogging functions
This module provides logging functions. It is actually a thin extension of Lwt_log_core, adding logging destinations that are only available on Unix and Windows (such as files, standard output, and the system log). Being a thin extension, most of the functions in this module are actually the ones in Lwt_log_core. They are not repeated on this page, however, so please read both Lwt_log_core and this page for a complete understanding.
Here is a simple, self-contained usage example:
let () =
Lwt_log.default :=
Lwt_log.channel
~template:"$(date).$(milliseconds) [$(level)] $(message)"
~close_mode:`Keep
~channel:Lwt_io.stdout
();
Lwt_log.add_rule "*" Lwt_log.Info;
Lwt_main.run begin
Lwt_log.info "Hello world!"
end
(* ocamlfind opt -linkpkg -package lwt.unix log_example.ml && ./a.out *)As an alternative to this module, we suggest trying Logs_lwt from the logs library.
include module type of Lwt_log_core
with type level = Lwt_log_core.level
and type logger = Lwt_log_core.logger
and type section = Lwt_log_core.section
and type template = Lwt_log_core.template
and module Section = Lwt_log_core.SectionThis module provides functions to deal with logging. It support:
type level = Lwt_log_core.level = | DebugDebugging message. They can be automatically removed by the syntax extension.
*)| InfoInformational message. Suitable to be displayed when the program is in verbose mode.
*)| Notice| WarningSomething strange happend
*)| ErrorAn error message, which should not means the end of the program.
*)| FatalA fatal error happened, in most cases the program will end after a fatal error.
*)Type of log levels. A level determines the importance of a message
type logger = Lwt_log_core.loggerType of a logger. A logger is responsible for dispatching messages and storing them somewhere.
type section = Lwt_log_core.sectionEach logging message has a section. Sections can be used to structure your logs. For example you can choose different loggers according to the section.
Each section carries a level, and messages with a lower log level than than the section level will be dropped.
Section levels are initialised using the contents of the LWT_LOG environment variable, which must contain one or more rules of the form pattern -> level separated by ";". Where pattern is a string that may contain *.
For example, if LWT_LOG contains:
access -> warning;
foo[*] -> errorthen the level of the section "access" is Warning and the level of any section matching "foo[*]" is Error.
If the pattern is omited in a rule then the pattern "*" is used instead, so LWT_LOG may just contain "debug" for instance.
By default, the following rule apply : "* -> notice"
val string_of_level : level -> stringval level_of_string : string -> level optionReset the rules set when parsing the LWT_LOG environment variable using this string.
load_rules parses the rules string and validates the rules before loading them. If fail_on_error is true, invalid rules will cause this function to raise Failure and leave existing rules unchanged. If fail_on_error is false (this is the default), it tries to load as many rules as possible and ignore invalid ones. If the rules string itself cannot be parsed, existing rules are always left unchanged.
Example:
Lwt_log_core.load_rules ~fail_on_error:true "* -> nosuchlevel" (* Raises Failure *)
Lwt_log_core.load_rules "* -> info"val add_rule : string -> level -> unitadd_rule pattern level adds a rule for sections logging levels. The rule is added before all other rules. It takes effect immediately and affects all sections for which the level has not been set explicitly with Section.set_level. pattern may contains *. For example:
Lwt_log_core.add_rule "lwt*" Lwt_log_core.Infoval append_rule : string -> level -> unitappend_rule pattern level adds the given rule after all other rules. For example to set the default fallback rule:
Lwt_log_core.append_rule "*" Lwt_log_core.Infoval log :
?exn:exn ->
?section:section ->
?location:(string * int * int) ->
?logger:logger ->
level:level ->
string ->
unit Lwt.tlog ?section ?logger ~level message logs a message.
section defaults to Section.main. If logger is not specified, then the default one is used instead (see default).
If exn is provided, then its string representation (= Printexc.to_string exn) will be append to the message, and if possible the backtrace will also be logged.
location contains the location of the logging directive, it is of the form (file_name, line, column).
val log_f :
?exn:exn ->
?section:section ->
?location:(string * int * int) ->
?logger:logger ->
level:level ->
('a, unit, string, unit Lwt.t) format4 ->
'alog_f is the same as log except that it takes a format string
val ign_log :
?exn:exn ->
?section:section ->
?location:(string * int * int) ->
?logger:logger ->
level:level ->
string ->
unitSame as log but ignore the resulting thread.
val ign_log_f :
?exn:exn ->
?section:section ->
?location:(string * int * int) ->
?logger:logger ->
level:level ->
('a, unit, string, unit) format4 ->
'aSame as log_f but ignore the resulting thread.
The following functions are the same as log except that their name determines which level is used.
For example info msg is the same as log ~level:Info msg.
module Section = Lwt_log_core.SectionSections
type template = Lwt_log_core.templateA template is for generating log messages.
It is a string which may contains variables of the form $(var), where var is one of:
message which will be replaced by the message emitedlevel which will be replaced by a string representation of the levelsection which will be replaced by the name of the message's sectionloc-file which will be replaced by the file name of the calling logging functionloc-line which will be replaced by the line number of the calling logging functionloc-column which will be replaced by the column number of the calling logging functionFor example:
"$(name): $(message)""$(name): $(loc-file): $(loc-line): $(loc-column): $(message)"val location_key : (string * int * int) Lwt.keyThe key for storing current location.
val make :
output:(section -> level -> string list -> unit Lwt.t) ->
close:(unit -> unit Lwt.t) ->
loggermake ~output ~close creates a new logger.
The default logger. It is used as default when no one is specified. If Lwt_core is linked (in the package lwt.unix) the default logger sends all messages to standard error. Otherwise the default logger is null.
broadcast loggers is a logger which send messages to all the given loggers.
Note: closing a broadcast logger does not close its components.
dispatch f is a logger which dispatch logging instructions to different logger according to their level and/or section.
Here is an example:
let access_logger = Lwt_log.file "access.log"
and error_logger = Lwt_log.file "error.log" in
Lwt_log_core.dispatch
(fun section level ->
match Lwt_log_core.Section.name section, level with
| "access", _ -> access_logger
| _, Lwt_log_core.Error -> error_logger)val null : loggerLogger which drops everything
val render :
buffer:Buffer.t ->
template:template ->
section:section ->
level:level ->
message:string ->
unitSame as Lwt_log_core.render, except that the template may also contain the following variables:
date, which will be replaced with the current local date and time,milliseconds, which will be replaced by the fractional part of the current Unix time, to millisecond accuracy.For example:
"$(date) $(name)[$(pid)]: $(message)""$(date).$(milliseconds) $(name)[$(pid)]: $(message)""$(date): $(loc-file): $(loc-line): $(loc-column): $(message)"type syslog_facility = [ | `Auth| `Authpriv| `Cron| `Daemon| `FTP| `Kernel| `Local0| `Local1| `Local2| `Local3| `Local4| `Local5| `Local6| `Local7| `LPR| `Mail| `News| `Syslog| `User| `UUCP| `NTP| `Security| `Console ]Syslog facility. Look at the SYSLOG(3) man page for a description of syslog facilities
val syslog :
?template:template ->
?paths:string list ->
facility:syslog_facility ->
unit ->
loggersyslog ?template ?paths ~facility () creates an logger which send message to the system logger.
val file :
?template:template ->
?mode:[ `Truncate | `Append ] ->
?perm:Unix.file_perm ->
file_name:string ->
unit ->
logger Lwt.tdesf_file ?template ?mode ?perm ~file_name () creates an logger which will write messages to file_name.
mode = `Truncate then the file is truncated and previous contents will be lost.mode = `Append, new messages will be appended at the end of the fileval channel :
?template:template ->
close_mode:[ `Close | `Keep ] ->
channel:Lwt_io.output_channel ->
unit ->
loggerchannel ?template ~close_mode ~channel () creates a logger from a channel.
If close_mode = `Close then channel is closed when the logger is closed, otherwise it is left open.