Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* First commit

  • Loading branch information...
commit e9b5b77b423dfc6918a3bb5b28248883f42b9222 0 parents
@joehewitt authored
Showing with 122 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +13 −0 LICENSE
  3. +36 −0 README.md
  4. +54 −0 logger.js
  5. +18 −0 package.json
1  .gitignore
@@ -0,0 +1 @@
+node_modules
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright 2011 Joe Hewitt
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
36 README.md
@@ -0,0 +1,36 @@
+express-logger
+===============
+
+Express middleware for log files.
+
+This middleware builds on the built-in Express logger. It will automatically rotate your logs and archive the old logs daily.
+
+Usage
+------------
+
+Works just like express.logger, except that you give it a path instead of a stream.
+
+ var logger = require('express-logger');
+ server.use(logger({path: "/path/to/logfile.txt"}));
+
+Installation
+------------
+
+ $ npm install express-logger
+
+License
+-------
+
+Copyright 2011 Joe Hewitt
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
54 logger.js
@@ -0,0 +1,54 @@
+
+var fs = require('fs');
+var util = require('util');
+var datetime = require('datetime');
+var express = require('express');
+
+// *************************************************************************************************
+
+var defaultInterval = 60*60*24*1000;
+var defaultDateFormat = '%Y-%m-%d';
+
+// *************************************************************************************************
+
+module.exports = function(options) {
+ var logStream = fs.createWriteStream(options.path, {flags: 'a'});
+ var logger = express.logger({stream: logStream, format: options.format});
+
+ // XXXjoe Need to expose a way to cancel this timer
+ var archiveInterval = setInterval(function() {
+ archiveLogs(logStream, options.path, options.dateFormat);
+ }, options.interval || defaultInterval);
+
+ return logger;
+}
+
+function archiveLogs(logStream, logPath, dateFormat) {
+ var logDate = datetime.format(new Date(), dateFormat || defaultDateFormat);
+ var ext = path.extname(logPath);
+ var name = path.basename(logPath, ext);
+ var dir = path.dirname(logPath);
+ var newPath = path.join(dir, name + '-' + logDate + ext);
+
+ copyFile(logPath, newPath, function(err) {
+ if (!err) {
+ fs.truncateSync(logStream.fd, 0);
+ } else {
+ console.error("Unable to archive logs: " + err);
+ }
+ });
+}
+
+function copyFile(src, dst, cb) {
+ fs.stat(dst, function(err) {
+ if (!err) { cb(new Error(dst + " already exists.")); return; }
+
+ fs.stat(src, function (err) {
+ if (err) { cb(err); return }
+
+ var is = fs.createReadStream(src);
+ var os = fs.createWriteStream(dst);
+ util.pump(is, os, cb);
+ });
+ });
+}
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "express-logger",
+ "description": "Express middleware for log files.",
+ "version": "0.0.1",
+ "homepage": "http://github.com/joehewitt/express-logger",
+ "repository": {
+ "type": "git",
+ "url" : "http://github.com/joehewitt/express-logger.git"
+ },
+ "keywords": ["express", "logger"],
+ "author": "Joe Hewitt <joe@joehewitt.com>",
+ "dependencies": {
+ "datetime": "",
+ "express": ""
+ },
+ "engines": { "node": ">=0.4.0" },
+ "main": "./logger"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.