-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Refactor logging to provide common logger from LoggerAdapter #2478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0b67029
Refactor logging to provide common logger from LoggerAdapter
flovilmart a75778a
Adds additional logging levels as requirements
flovilmart 52e40bf
Adds tests for logging configuration
flovilmart 54fcbe4
removes flaky test
flovilmart 4f1f346
investigate...
flovilmart ce11e74
further investigation
flovilmart 141c2a6
Adds silent option to disable console output
flovilmart ecc9de9
Restores logs with VERBOSE in tests
flovilmart 5276ab2
Expose controller instead of adapter, reduces method requirements for…
flovilmart 3d1539d
Shuffles initializations around
flovilmart ff92ded
Fix doc
flovilmart 354316e
Load cloudCode last to make sure the logger is available
flovilmart 10a5af1
Adds test to make sure we can load an adapter from npm module
flovilmart 07615c7
extract defaults
flovilmart 91758cc
Adds defaultMongoURI to defaults
flovilmart ca94f51
fix defaults values
flovilmart ea16048
Proper error for PG failures
flovilmart f2c4770
Disable flaky test
flovilmart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import winston from 'winston'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import DailyRotateFile from 'winston-daily-rotate-file'; | ||
import _ from 'lodash'; | ||
import defaults from '../../defaults'; | ||
|
||
const logger = new winston.Logger(); | ||
const additionalTransports = []; | ||
|
||
function updateTransports(options) { | ||
let transports = Object.assign({}, logger.transports); | ||
if (options) { | ||
let silent = options.silent; | ||
delete options.silent; | ||
if (_.isNull(options.dirname)) { | ||
delete transports['parse-server']; | ||
delete transports['parse-server-error']; | ||
} else if (!_.isUndefined(options.dirname)) { | ||
transports['parse-server'] = new (DailyRotateFile)( | ||
Object.assign({ | ||
filename: 'parse-server.info', | ||
name: 'parse-server', | ||
}, options)); | ||
transports['parse-server-error'] = new (DailyRotateFile)( | ||
Object.assign({ | ||
filename: 'parse-server.err', | ||
name: 'parse-server-error', | ||
level: 'error' | ||
}, options)); | ||
} | ||
|
||
transports.console = new (winston.transports.Console)( | ||
Object.assign({ | ||
colorize: true, | ||
name: 'console', | ||
silent | ||
}, options)); | ||
} | ||
// Mount the additional transports | ||
additionalTransports.forEach((transport) => { | ||
transports[transport.name] = transport; | ||
}); | ||
logger.configure({ | ||
transports: _.values(transports) | ||
}); | ||
} | ||
|
||
export function configureLogger({ | ||
logsFolder = defaults.logsFolder, | ||
jsonLogs = defaults.jsonLogs, | ||
logLevel = winston.level, | ||
verbose = defaults.verbose, | ||
silent = defaults.silent } = {}) { | ||
|
||
if (verbose) { | ||
logLevel = 'verbose'; | ||
} | ||
|
||
winston.level = logLevel; | ||
const options = {}; | ||
|
||
if (logsFolder) { | ||
if (!path.isAbsolute(logsFolder)) { | ||
logsFolder = path.resolve(process.cwd(), logsFolder); | ||
} | ||
try { | ||
fs.mkdirSync(logsFolder); | ||
} catch (exception) {} | ||
} | ||
options.dirname = logsFolder; | ||
options.level = logLevel; | ||
options.silent = silent; | ||
|
||
if (jsonLogs) { | ||
options.json = true; | ||
options.stringify = true; | ||
} | ||
updateTransports(options); | ||
} | ||
|
||
export function addTransport(transport) { | ||
additionalTransports.push(transport); | ||
updateTransports(); | ||
} | ||
|
||
export function removeTransport(transport) { | ||
let transportName = typeof transport == 'string' ? transport : transport.name; | ||
let transports = Object.assign({}, logger.transports); | ||
delete transports[transportName]; | ||
logger.configure({ | ||
transports: _.values(transports) | ||
}); | ||
_.remove(additionalTransports, (transport) => { | ||
return transport.name === transportName; | ||
}); | ||
} | ||
|
||
export { logger, addTransport, configureLogger, removeTransport }; | ||
export default logger; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
output to console or don't catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May fail if the folder exists, so we just let go