Pye::MongoDB - Log with Pye on top of MongoDB
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
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
session_id
date - the date the messages was logged, in ISODate format, always exists
date
text - the text of the message, a string, always exists
text
data - supplemental JSON structure, optional
data
An index on the session_id field will automatically be created.
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:
mongo
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.
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.
host
find_master
username
password
MongoDB::MongoClient
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
safe
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.).
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.
Pye
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.
\%data
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 }.
sort
{ _id => 1 }
{ date => -1 }
Pye::MongoDB requires no configuration files or environment variables.
Pye::MongoDB
Pye::MongoDB depends on the following CPAN modules:
version
Carp
MongoDB
Role::Tiny
Tie::IxHash
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.
bug-Pye-MongoDB@rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Pye::MongoDB
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Pye-MongoDB
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Pye-MongoDB
CPAN Ratings
http://cpanratings.perl.org/d/Pye-MongoDB
Search CPAN
http://search.cpan.org/dist/Pye-MongoDB/
Ido Perlmuter <ido@ido50.net>
Copyright (c) 2015, Ido Perlmuter ido@ido50.net.
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.
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.
To install Pye::MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pye::MongoDB
CPAN shell
perl -MCPAN -e shell install Pye::MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.