Skip to content
Browse files

[api] add clear() method

  • Loading branch information...
1 parent 3bec167 commit 23fbb2635654165bd6c442912bb06c2e64341847 @chjj chjj committed May 15, 2012
Showing with 116 additions and 1 deletion.
  1. +54 −0 lib/winston/logger.js
  2. +22 −0 lib/winston/transports/file.js
  3. +40 −1 lib/winston/transports/http.js
View
54 lib/winston/logger.js
@@ -347,6 +347,60 @@ Logger.prototype.stream = function (options) {
};
//
+// ### function clear (options, callback)
+// #### @options {Object} Stream options for this instance.
+// #### @callback {function} Continuation to respond to when complete.
+// Clear persistent logs at transport.
+//
+Logger.prototype.clear = function (options, callback) {
+ if (typeof options === 'function') {
+ callback = options;
+ options = null;
+ }
+
+ var self = this,
+ options = options || {},
+ error = null;
+
+ if (options.transport) {
+ var transport = options.transport;
+ delete options.transport;
+ transport = this.transports[transport];
+ if (!transport.clear) {
+ if (callback) callback(new Error('No clear() method found.'));
+ return;
+ }
+ transport.clear(options, function (err) {
+ if (callback) callback(err);
+ });
+ }
+
+ var transports = this._names.map(function (name) {
+ return self.transports[name];
+ }).filter(function (transport) {
+ return !!transport.clear;
+ });
+
+ function clear(transport, next) {
+ transport.clear(options, function (err) {
+ if (err) {
+ if (!error) error = err;
+ else (Array.isArray(error)) error.push(err);
+ else error = [error, err];
+ }
+ next();
+ });
+ }
+
+ //
+ // Iterate over the transports in parallel.
+ //
+ async.forEach(transports, clear, function () {
+ if (callback) callback(error);
+ });
+};
+
+//
// ### function close ()
// Cleans up resources (streams, event listeners) for all
// transports associated with this instance (if necessary).
View
22 lib/winston/transports/file.js
@@ -340,6 +340,28 @@ File.prototype.stream = function (options) {
};
//
+// ### function clear (options, callback)
+// #### @options {Object} Stream options for this instance.
+// #### @callback {function} Continuation to respond to when complete.
+// Clear persistent logs at transport.
+//
+File.prototype.clear = function (options, callback) {
+ if (typeof options === 'function') {
+ callback = options;
+ options = null;
+ }
+
+ var self = this,
+ options = options || {},
+ file = path.join(this.dirname, this.filename);
+
+ fs.writeFile(file, '', function (err) {
+ self._createStream();
+ if (callback) callback(err);
+ });
+}:
+
+//
// ### function open (callback)
// #### @callback {function} Continuation to respond to when complete
// Checks to see if a new file needs to be created based on the `maxsize`
View
41 lib/winston/transports/http.js
@@ -18,7 +18,7 @@ var Http = exports.Http = function (options) {
this.port = options.port;
this.auth = options.auth;
this.path = options.path || '';
-
+
if (!this.port) {
this.port = this.ssl ? 443 : 80;
}
@@ -198,3 +198,42 @@ Http.prototype.stream = function (options) {
return stream;
};
+
+//
+// ### function clear (options, callback)
+// #### @options {Object} Stream options for this instance.
+// #### @callback {function} Continuation to respond to when complete.
+// Clear persistent logs at transport.
+//
+Http.prototype.clear = function (options, callback) {
+ if (typeof options === 'function') {
+ callback = options;
+ options = null;
+ }
+
+ var self = this,
+ options = options || {};
+
+ options = {
+ method: 'clear',
+ params: options
+ };
+
+ this._request(options, function (err, res, body) {
+ if (res && res.statusCode !== 200) {
+ err = new Error('HTTP Status Code: ' + res.statusCode);
+ }
+
+ if (err) return callback(err);
+
+ if (typeof body === 'string') {
+ try {
+ body = JSON.parse(body);
+ } catch (e) {
+ return callback(e);
+ }
+ }
+
+ callback(body);
+ });
+}:

0 comments on commit 23fbb26

Please sign in to comment.
Something went wrong with that request. Please try again.