Log Derive
log-derive
provides a simple attribute macro that facilitates logs as part of the log
facade
Right now it contains two macros logfn
, logfn_inputs
these macros are only for functions but still have a lot of power.
Use
The basic use of these macros is by putting one or both of them on top of the function like this: #[logfn(INFO)]
The logfn
macro is used to log the output of the function and logfn_inputs
is used to log the inputs.
Please notice, the arguments being logged must implement the Debug
trait.
(i.e. logfn
requires the output to be Debug
and logfn_inputs
require the inputs to be Debug
)
The macros will accept all log levels provided by the log
facade.
In logfn
if the function returns a Result
type the macro will accept the following additional attributes:
(ok = "LEVEL")
and (err = "LEVEL")
this can provide different log levels if the function failed or not.
By default the macro uses the following formatting to print the message:
logfn
: ("FUNCTION_NAME() => {:?}", return_val)
logfn_inputs
: "FUNCTION_NAME(a: {:?}, b: {:?})", a, b)
This can be easily changed using the fmt
attribute: #[logfn(LEVEL, fmt = "Important Result: {:}")
which will accept format strings similar to println!
.
Examples
use ;
#
;
#
;
#
#
#
# ;
#
# use Duration;