Created
July 8, 2013 07:44
-
-
Save openprojdev/5946943 to your computer and use it in GitHub Desktop.
Log apache2 logs in MongoDB, avoid apache log rotate, Async logging to MongoDB, piped log parser to mongoDB using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php5 | |
| <?php | |
| /* | |
| Apache2 Log format in Json | |
| LogFormat "{ \"@timestamp\": \"%{%Y-%m-%dT%H:%M:%S%z}t\", \"@fields\": { \"client\": \"%a\", \"duration_usec\": %D, \"status\": %s, \"request\": \"%U%q\", \"method\": \"%m\", \"referrer\": \"%{Referer}i\" } }" log_json | |
| CustomLog "|/var/www/mongolog.php" log_json | |
| */ | |
| //mongo server settings | |
| $mongo_server = 'SERVER_IP'; | |
| $mongo_db = 'server_logs'; // mongo db | |
| $mongo_collection = 'live'; // mongo collection | |
| ob_implicit_flush (true); | |
| while (FALSE !== ($line = fgets(STDIN))) { | |
| if (empty($line)) continue; | |
| $in_log = json_decode( $line, true ); | |
| $m = new Mongo( $mongo_server ); | |
| $db = $m->selectDB( $mongo_db ); | |
| $collection = new MongoCollection( $db, $mongo_collection ); | |
| if ( $in_log != null ) { | |
| $collection->insert( $in_log ); | |
| } else { | |
| $collection->insert( array( 'json_error_str' => (string)$line ) ); | |
| } | |
| } | |
| exit(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment