Records

Records

Records store information about log events (e.g., message, timestamp, log level) that is used by Formatters to format log messages. A record behaves as a dictionary-like container with Symbol keys, and you can access the properties of a Record by using getproperty (i.e., record.msg).

By default, any subtypes of Record will treat its fields as keys. Non-standard subtypes of Record should implement getproperty(::MyRecord, ::Symbol) and key-value pair iteration.

AttributeRecords

An AttributeRecord is an abstract subtype of Record that lazily evaluates its properties. Fields are stored as Attributes, which will evaluate a function and cache the result the first time it is read.

By default, any subtypes of AttributeRecord will expect its fields to be Attributes. Non-standard subtypes of AttributeRecord should implement Base.getproperty(::MyRecord, ::Symbol) and key-value pair iteration, where the values have been extracted from Attributes using get.

Custom Record Types

While the DefaultRecord in Memento (a standard AttributeRecord) provides many of the keys and values needed for most logging applications, you may need to implement your own Record type. For example, if you're running a julia application on a cloud service provider like Amazon's EC2 you might want to include some general information about the resource your code is running on, which might result in a custom Record type that looks like:

# TODO: Fix this example.
mutable struct EC2Record <: AttributeRecord
    date::Attribute
    level::Attribute
    levelnum::Attribute
    msg::Attribute
    name::Attribute
    pid::Attribute
    lookup::Attribute
    stacktrace::Attribute
    instance_id::Attribute
    public_ip::Attribute
    iam_user::Attribute

    function EC2Record(name::AbstractString, level::AbstractString, levelnum::Int, msg)
        time = now()
        trace = Attribute{StackTrace}(get_trace)

        EC2Record(
            Attribute{DateTime}(() -> round(time, Dates.Second)),
            Attribute(level),
            Attribute(levelnum),
            Attribute{AbstractString}(msg),
            Attribute(name),
            Attribute(getpid()),
            Attribute{StackFrame}(get_lookup(trace)),
            trace,
            Attribute(ENV["INSTANCE_ID"]),
            Attribute(ENV["PUBLIC_IP"]),
            Attribute(ENV["IAM_USER"]),
        )
    end
end

NOTE: The above example simply assumes that you have some relevant environment variables set on the machine, but you could also query Amazon for that information.