Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 2 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
12 lib/Log/Message/Structured.pm
@@ -1,7 +1,6 @@
package Log::Message::Structured;
use MooseX::Role::WithOverloading;
use MooseX::Storage;
-use Sys::Hostname ();
use namespace::clean -except => 'meta';
our $VERSION = '0.006';
@@ -25,12 +24,6 @@ has epochtime => (
sub BUILD {}
after BUILD => sub { shift()->date };
-has hostname => (
- is => 'ro',
- default => sub { Sys::Hostname::hostname() },
- $GETOPT ? ( traits => [qw/ NoGetopt /] ) : (),
-);
-
requires 'stringify';
1;
@@ -58,6 +51,7 @@ Log::Message::Structured - Simple structured log messages
# Components must be consumed seperately
with qw/
Log::Message::Structured::Component::Date
+ Log::Message::Structured::Component::Hostname
/;
has foo => ( is => 'ro', required => 1 );
@@ -99,10 +93,6 @@ L<Log::Message::Structured::Component::Date>
The basic Log::Message::Structured role provides the following read only attributes:
-=head2 hostname
-
-The host name of the host the event was generated on. Defaults to the hostname as returned by L<Sys::Hostname>.
-
=head1 epochtime
The date and time on which the event occurred, as an no of seconds since Jan 1st 1970 (i.e. the output of time())
View
3 lib/Log/Message/Structured/Component/Date.pm
@@ -37,6 +37,9 @@ Log::Message::Structured::Component::Date
# Note: you MUST compose these together as they depend on methods in each other
with qw/
Log::Message::Structured
+ /;
+ # Components must be consumed seperately
+ with qw/
Log::Message::Structured::Component::Date
/;
View
69 lib/Log/Message/Structured/Component/Hostname.pm
@@ -0,0 +1,69 @@
+package Log::Message::Structured::Component::Hostname;
+use Moose::Role;
+use namespace::autoclean;
+
+use Sys::Hostname ();
+
+my $GETOPT = do { local $@; eval { require MooseX::Getopt; 1 } };
+
+has hostname => (
+ is => 'ro',
+ default => sub { Sys::Hostname::hostname() },
+ $GETOPT ? ( traits => [qw/ NoGetopt /] ) : (),
+);
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Log::Message::Structured::Component::Hostname
+
+=head1 SYNOPSIS
+
+ package MyLogEvent;
+ use Moose;
+ use namespace::autoclean;
+
+ with qw/
+ Log::Message::Structured
+ /;
+ # Components must be consumed seperately
+ with qw/
+ 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,"hostname":"mymachine.domain"}
+
+=head1 DESCRIPTION
+
+Provides a C<'hostname'> attribute to the consuming class ( probably
+L<Log::Message::Structured>).
+
+=head1 ATTRIBUTES
+
+=head2 hostname
+
+The host name of the host the event was generated on. Defaults to the hostname
+as returned by L<Sys::Hostname>.
+
+=head1 AUTHOR AND COPYRIGHT
+
+Damien Krotkine (dams) C<< <dams@cpan.org> >>.
+
+=head1 LICENSE
+
+Licensed under the same terms as perl itself.
+
+=cut

No commit comments for this range

Something went wrong with that request. Please try again.