The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Pye::MongoDB - Log with Pye on top of MongoDB

SYNOPSIS

        use Pye::MongoDB;

        my $pye = Pye::MongoDB->new(
                host => 'mongodb://logserver1:27017,logserver2:27017',
                find_master => 1,
                database => 'log_db',
                collection => 'myapp_log'
        );

        # now start logging
        $pye->log($session_id, "Some log message", { data => 'example data' });

        # inspect the logs from the command line
        pye -b MongoDB -d log_db -c myapp_log

DESCRIPTION

This package provides a MongoDB backend for the Pye logging system. This is currently the easiest backend to use, since no setup is needed in order to start logging with it.

Messages will be stored in a MongoDB database with the following keys:

  • session_id - the session ID, a string, always exists

  • date - the date the messages was logged, in ISODate format, always exists

  • text - the text of the message, a string, always exists

  • data - supplemental JSON structure, optional

An index on the session_id field will automatically be created.

NOTES AND RECOMMENDATIONS

As of this writing (MongoDB v2.6), MongoDB is kind of a storage guzzler. You might find it useful to create a TTL index on the log collection. For example, the following line (entered into the mongo shell) will create a time-to-live index of 2 days on a log collection:

        db.log_collection.ensureIndex({ date: 1 }, { expireAfterSeconds: 172800 })

Alternatively, you could make the collection capped and limit it by size. Note, however, that the _remove_session_logs() method will not work in that case.

Also, consider using TokuMX as a drop-in replacement for MongoDB. It it faster, uses much less storage space and supports ACID transactions.

USING THE pye COMMAND LINE UTILITY

The pye command line utility, used to inspect logs, provides all command line options to the "new( [ %options ] )" constructor, which in turn passes anything to MongoDB::MongoClient. This means that if your database has replication, or requires authentication, you can provide these options from the command line.

For example:

        pye -b MongoDB
            --host mongodb://server1:27017,server2:27017
            --find_master=1
            -d log_db
            -c myapp_log
            --username log_user
            --password very_secret

host, find_master, username and password in this example will be passed to MongoDB::MongoClient.

CONSTRUCTOR

new( [ %options ] )

Create a new instance of this class. All options are optional.

  • database - the name of the database, defaults to "logs"

  • collection (or table) - the name of the collection, defaults to "logs"

  • be_safe - whether to enable the safe flag when inserting log messages, defaults to a false value

Any other option you provide will be passed to MongoDB::MongoClient, so pass anything needed in order to connect to the database server (such as host, find_master, etc.).

OBJECT METHODS

The following methods implement the Pye role, so you should refer to Pye for their documentation. Some methods, however, have some MongoDB-specific notes, so keep reading.

log( $session_id, $text, [ \%data ] )

If \%data is provided, this module will traverse it recursively, replacing any hash-key that contains dots with semicolons, as MongoDB does not support dots in field names.

session_log( $session_id )

list_sessions( [ \%opts ] )

Takes all options defined by Pye. The sort option, however, takes a MongoDB sorting definition, that is a hash-ref, e.g. { _id => 1 }. This will default to { date => -1 }.

CONFIGURATION AND ENVIRONMENT

Pye::MongoDB requires no configuration files or environment variables.

DEPENDENCIES

Pye::MongoDB depends on the following CPAN modules:

BUGS AND LIMITATIONS

Please report any bugs or feature requests to bug-Pye-MongoDB@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Pye-MongoDB.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Pye::MongoDB

You can also look for information at:

AUTHOR

Ido Perlmuter <ido@ido50.net>

LICENSE AND COPYRIGHT

Copyright (c) 2015, Ido Perlmuter ido@ido50.net.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either version 5.8.1 or any later version. See perlartistic and perlgpl.

The full text of the license can be found in the LICENSE file included with this module.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.