-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: consistent logging messages see #286
- Loading branch information
1 parent
9fa6c50
commit 50cfcb9
Showing
4 changed files
with
169 additions
and
97 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }], | ||
no-console: ["error", { allow: ["warn", "error", "info"] }] */ | ||
/* jshint esversion: 6, asi: true, node: true */ | ||
// logging.js | ||
// private | ||
|
||
const debug = require('debug'); | ||
const util = require('util'); | ||
|
||
/** | ||
* generate consistent prefix for log messages | ||
* with epress session id and socket session id | ||
* @param {object} socket Socket information | ||
*/ | ||
function prefix(socket) { | ||
return `(${socket.request.sessionID}/${socket.id})`; | ||
} | ||
|
||
// public | ||
function webssh2debug(socket, msg) { | ||
debug('WebSSH2')(`${prefix(socket)} ${msg}`); | ||
} | ||
|
||
/** | ||
* audit log to console | ||
* @param {object} socket Socket information | ||
* @param {object} msg log message | ||
*/ | ||
function auditLog(socket, msg) { | ||
console.info(`WebSSH2 ${prefix(socket)} AUDIT: ${msg}`); | ||
} | ||
|
||
/** | ||
* logs error to socket client (if connected) | ||
* and console. | ||
* @param {object} socket Socket information | ||
* @param {string} myFunc Function calling this function | ||
* @param {object} err error object or error message | ||
*/ | ||
function logError(socket, myFunc, err) { | ||
console.error(`WebSSH2 ${prefix(socket)} ERROR: ${myFunc}: ${err}`); | ||
webssh2debug(socket, `logError: ${myFunc}: ${util.inspect(err)}`); | ||
if (!socket.request.session) return; | ||
socket.emit('ssherror', `SSH ${myFunc}: ${err}`); | ||
} | ||
|
||
module.exports = { logError, auditLog, webssh2debug }; |
This file contains 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