Log::Message::Structured - Simple structured log messages
package MyLogEvent; use Moose; use namespace::autoclean; with qw/ Log::Message::Structured Log::Message::Structured::Stringify::AsJSON /; # Components must be consumed seperately with qw/ Log::Message::Structured::Component::Date Log::Message::Structured::Component::Hostname /; has foo => ( is => 'ro', required => 1 ); ... elsewhere ... use aliased 'My::Log::Event'; $logger->log(message => Event->new( foo => "bar" )); # Logs: {"__CLASS__":"MyLogEvent","foo":1,"date":"2010-03-28T23:15:52Z","hostname":"mymachine.domain"}
Logging lines to a file is a fairly useful and traditional way of recording what's going on in your application.
However, if you have another use for the same sort of data (for example, sending to another process via a message queue, or storing in KiokuDB), then you can be needlessly repeating your data marshalling.
Log::Message::Structured is a VERY VERY SIMPLE set of roles to help you make small structured classes which represent 'something which happened', that you can then either pass around in your application, log in a traditional manor as a log line, or serialize to JSON for transmission over the network.
something which happened
The consuming class can include components, that will provide additional attributes. Here is a list of the components included in the basic distribution. More third party components may be available on CPAN.
Log::Message::Structured::Component::Date
Log::Message::Structured::Component::Hostname
The basic Log::Message::Structured role provides the following read only attributes:
The date and time on which the event occurred, as an no of seconds since Jan 1st 1970 (i.e. the output of time())
The only non-accessor methods provided are those composed from MooseX::Storage related to serialization and deserialization.
Return the instance as a JSON string.
Inflate an instance of the class from a JSON string.
Return the instance data as a plain data structure (hashref).
Inflate an instance from a plain data structure (hashref).
An empty build method (which will be silently discarded if you have one in your class) is provided, so that additional components can wrap it (to farce lazy attributes to be built).
You must implement a stringify method, or compose a stringification role for all Log::Message::Structured events. This is so that events will always be meaningfully loggable be printing them to STDOUT or STDERR, or logging them in a traditional way in a file.
namespace::autoclean does not work correctly with roles that supply overloading. Therefore you should instead use:
use namespace::clean -except => 'meta';
instead in all classes using Log::Message::Structured.
Tomas Doran (t0m) <bobtfish@bobtfish.net>.
<bobtfish@bobtfish.net>
Licensed under the same terms as perl itself.
To install Log::Message::Structured, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Message::Structured
CPAN shell
perl -MCPAN -e shell install Log::Message::Structured
For more information on module installation, please visit the detailed CPAN module installation guide.