-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Compatibility with latest Meteor release - NPM & Meteor dependencies update - Move codebase to ES6 (*No more CoffeeScript*) - Overall codebase enhancements - API is slightly changed - please, see the docs - __Breaking:__ `LoggerFile` is not exported to global scope use - ES6 `import`
- Loading branch information
1 parent
dcda499
commit 0eba6be
Showing
4 changed files
with
236 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,154 @@ | ||
import { _ } from 'meteor/underscore'; | ||
import { Meteor } from 'meteor/meteor'; | ||
import { Logger } from 'meteor/ostrio:logger'; | ||
import { check, Match } from 'meteor/check'; | ||
|
||
let fs = {}; | ||
const NOOP = () => {}; | ||
|
||
if (Meteor.isServer) { | ||
fs = require('fs-extra'); | ||
} | ||
|
||
/* | ||
* @class LoggerFile | ||
* @summary File (FS) adapter for ostrio:logger (Logger) | ||
*/ | ||
class LoggerFile { | ||
constructor(logger, options = {}) { | ||
check(logger, Match.OneOf(Logger, Object)); | ||
check(options, Match.Optional(Object)); | ||
|
||
this.logger = logger; | ||
this.options = options; | ||
|
||
if (Meteor.isServer) { | ||
/* fileNameFormat - Log file name */ | ||
if (this.options.fileNameFormat) { | ||
if (!_.isFunction(this.options.fileNameFormat)) { | ||
throw new Meteor.Error('[LoggerFile] [options.fileNameFormat] Must be a Function!'); | ||
} | ||
} else { | ||
this.options.fileNameFormat = (time) => { | ||
let month = `${time.getMonth() + 1}`; | ||
if (month.length === 1) { | ||
month = '0' + month; | ||
} | ||
|
||
let date = `${time.getDate()}`; | ||
if (date.length === 1) { | ||
date = '0' + date; | ||
} | ||
|
||
let year = `${time.getFullYear()}`; | ||
if (year.length === 1) { | ||
year = '0' + year; | ||
} | ||
|
||
return `${date}-${month}-${year}.log`; | ||
}; | ||
} | ||
|
||
/* format - Log record format */ | ||
if (this.options.format) { | ||
if(!_.isFunction(this.options.format)) { | ||
throw new Meteor.Error('[LoggerFile] [options.format] Must be a Function!'); | ||
} | ||
} else { | ||
this.options.format = (time, level, message, data, userId) => { | ||
let month = `${time.getMonth() + 1}`; | ||
if (month.length === 1) { | ||
month = '0' + month; | ||
} | ||
let date = `${time.getDate()}`; | ||
if (date.length === 1) { | ||
date = '0' + date; | ||
} | ||
let year = `${time.getFullYear()}`; | ||
if (year.length === 1) { | ||
year = '0' + year; | ||
} | ||
let hours = `${time.getHours()}`; | ||
if (hours.length === 1) { | ||
hours = '0' + hours; | ||
} | ||
let mins = `${time.getMinutes()}`; | ||
if (mins.length === 1) { | ||
mins = '0' + mins; | ||
} | ||
let sec = `${time.getSeconds()}`; | ||
if (sec.length === 1) { | ||
sec = '0' + sec; | ||
} | ||
|
||
return `${date}-${month}-${year} ${hours}:${mins}:${sec} | [${level}] | Message: \"${message}\" | User: ${userId} | data: ${data}\r\n`; | ||
}; | ||
} | ||
|
||
/* path - Log's storage path */ | ||
if (this.options.path) { | ||
if (!_.isString(this.options.path)) { | ||
throw new Meteor.Error('[LoggerFile] [options.path] Must be a String!'); | ||
} | ||
} else { | ||
this.options.path = Meteor.rootPath + ((process.env.NODE_ENV === 'development') ? '/static/logs' : '/assets/app/logs'); | ||
} | ||
|
||
this.options.path = this.options.path.replace(/\/$/, ''); | ||
|
||
fs.ensureDir(`${this.options.path}`, (EDError) => { | ||
if (EDError) { | ||
throw new Meteor.Error('[LoggerFile] [options.path] Error:', EDError); | ||
} | ||
|
||
fs.writeFile(`${this.options.path}/test`, 'test', (WFError) => { | ||
if (WFError) { | ||
throw new Meteor.Error(`[LoggerFile] [options.path] ${this.options.path} is not writable!!!`, WFError); | ||
} | ||
fs.unlink(`${this.options.path}/test`, NOOP); | ||
}); | ||
}); | ||
} | ||
|
||
this.logger.add('File', (level, message, data = null, userId) => { | ||
if (Meteor.isServer) { | ||
const time = new Date(); | ||
let _data = null; | ||
|
||
if (data) { | ||
_data = this.logger.antiCircular(_.clone(data)); | ||
if (_.isString(_data.stackTrace)) { | ||
_data.stackTrace = _data.stackTrace.split(/\n|\\n|\r|\r\n/g); | ||
} | ||
_data = JSON.stringify(_data, false, 2); | ||
} | ||
|
||
fs.appendFile(`${this.options.path}/${this.options.fileNameFormat(time)}`, this.options.format(time, level, message, _data, userId), NOOP); | ||
} | ||
}, NOOP, false, false); | ||
} | ||
|
||
enable(rule = {}) { | ||
check(rule, { | ||
enable: Match.Optional(Boolean), | ||
client: Match.Optional(Boolean), | ||
server: Match.Optional(Boolean), | ||
filter: Match.Optional([String]) | ||
}); | ||
|
||
if (rule.enable == null) { | ||
rule.enable = true; | ||
} | ||
if (rule.client == null) { | ||
rule.client = true; | ||
} | ||
if (rule.server == null) { | ||
rule.server = true; | ||
} | ||
|
||
this.logger.rule('File', rule); | ||
return this; | ||
} | ||
} | ||
|
||
export { LoggerFile }; |
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,19 +1,24 @@ | ||
Package.describe({ | ||
name: 'ostrio:loggerfile', | ||
version: '1.1.2', | ||
version: '2.0.0', | ||
summary: 'Logging: Store application\'s logs into file (Server & Client support)', | ||
git: 'https://github.com/VeliovGroup/Meteor-logger-file', | ||
documentation: 'README.md' | ||
}); | ||
|
||
Package.onUse(function(api) { | ||
api.versionsFrom('1.0'); | ||
api.use('ostrio:[email protected]', 'server') | ||
api.use(['coffeescript', 'ostrio:[email protected]', 'check', 'underscore'], ['client', 'server']); | ||
api.addFiles('loggerfile.coffee', ['client', 'server']); | ||
api.export('LoggerFile'); | ||
api.versionsFrom('1.4'); | ||
api.use('ostrio:[email protected]', 'server'); | ||
api.use(['ecmascript', 'check', 'underscore', 'ostrio:[email protected]'], ['client', 'server']); | ||
api.mainModule('loggerfile.js', ['client', 'server']); | ||
}); | ||
|
||
Package.onTest(function(api) { | ||
api.use('tinytest'); | ||
api.use(['ecmascript', 'underscore', 'ostrio:[email protected]', 'ostrio:[email protected]']); | ||
api.addFiles('loggerfile-tests.js'); | ||
}); | ||
|
||
Npm.depends({ | ||
'fs-extra': '0.30.0' // NOTE: this package has dropped support for Node v0.10, since v0.29.0; Brought back Node v0.10 support in v0.30.0, official support will end 2016-10-01 | ||
}); | ||
'fs-extra': '3.0.1' | ||
}); |