-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Base code for logging. Need to write proper format for logs * Additional formats * trying to print error with red colour... * Completed logs in console * added logging of pathname * final changes in order to beautify * Name consistency * New paradigm of logging * fixed action representation in Logs * Fixed action's header setting * Creation page fix * Added contextId and pageId for error logging * Better naming * Minor changes to: naming, structure * Minor changes to: naming, structure * Minor changes to: naming, structure * bump version, minor refinements --------- Co-authored-by: Max Varlamov <[email protected]> Co-authored-by: Max Varlamov <[email protected]>
- Loading branch information
1 parent
cc6ff31
commit 645d27e
Showing
8 changed files
with
663 additions
and
407 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
node_modules | ||
.gitignore |
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,90 @@ | ||
const winston = require('winston'); | ||
|
||
const logPrinter = winston.format.printf((info) => { | ||
let log = `[${info.timestamp}] ${info.level}: ${info.message}`; | ||
|
||
if (info.stack) { | ||
log = `${log}` | ||
+ "\nRequest parameters:" | ||
+ `\n contextId: ${info.contextId}` | ||
+ `\n pageId: ${info.pageId}` | ||
+ `\n${info.stack}\n` | ||
} | ||
|
||
return log; | ||
}); | ||
|
||
const fileFormat = winston.format.combine( | ||
winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss:ms'}), | ||
winston.format.errors({stack: true}), | ||
logPrinter, | ||
); | ||
|
||
const consoleFormat = winston.format.combine( | ||
winston.format.errors({stack: true}), | ||
winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss:ms'}), | ||
winston.format.colorize({level: true}), | ||
logPrinter, | ||
); | ||
|
||
let logger; | ||
|
||
function createTransports(logLevel, logFilePath) { | ||
let transports = []; | ||
|
||
transports.push(new winston.transports.Console({ | ||
level: logLevel, | ||
format: consoleFormat, | ||
})); | ||
if (logFilePath !== undefined) { | ||
transports.push(new winston.transports.File({ | ||
level: logLevel, | ||
filename: logFilePath, | ||
format: fileFormat, | ||
})); | ||
} | ||
|
||
return transports; | ||
} | ||
|
||
exports.initLogger = function initLogger(logLevel, logFilePath) { | ||
const transports = createTransports(logLevel, logFilePath); | ||
|
||
logger = winston.createLogger({ | ||
level: logLevel, | ||
transports: transports, | ||
}); | ||
} | ||
|
||
function getBody(body) { | ||
if (body instanceof Buffer) { // Action request | ||
body = body.toString() | ||
} else { // Other requests | ||
body = JSON.stringify(body, null, 8); | ||
} | ||
|
||
return body; | ||
} | ||
|
||
exports.HTTPFormat = function HTTPFormat(tokens, req, res) { | ||
const reqContextId = req.query["contextId"]; | ||
const reqPageId = req.query["pageId"]; | ||
const resContextId = res.get('scrapy-puppeteer-service-context-id'); | ||
const closePage = req.query["closePage"] === "1"; | ||
|
||
const url = req.baseUrl || req.originalUrl || req.url; | ||
const queryIndex = url.indexOf('?'); | ||
const pathname = queryIndex !== -1 ? url.slice(1, queryIndex) : url.slice(1); | ||
|
||
return `${pathname} (${tokens.status(req, res)})` | ||
+ "\nRequest parameters:" | ||
+ `${reqContextId ? `\n contextId: ${reqContextId}` : ""}` | ||
+ `${reqPageId ? `\n pageId: ${reqPageId}` : ""}` | ||
+ `\n closePage: ${closePage}` | ||
+ `\n body: ${getBody(req.body)}` | ||
+ `${!reqContextId && resContextId ? `\nCreated page with ${resContextId} contextId` : ""}`; | ||
} | ||
|
||
exports.getLogger = function getLogger() { | ||
return logger; | ||
} |
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
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
Oops, something went wrong.