Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/logplease.min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ var Logger = function () {
}, {
key: '_write',
value: function _write(level, text) {
if ((this.options.filename || GlobalLogfile) && !this.fileWriter && isNodejs) this.fileWriter = fs.openSync(this.options.filename || GlobalLogfile, this.options.appendFile ? 'a+' : 'w+');
if ((this.options.filename || GlobalLogfile) && !this.fileWriter && (isNodejs || isElectronRenderer)) this.fileWriter = fs.openSync(this.options.filename || GlobalLogfile, this.options.appendFile ? 'a+' : 'w+');

var format = this._format(level, text);
var unformattedText = this._createLogMessage(level, text);
var formattedText = this._createLogMessage(level, text, format.timestamp, format.level, format.category, format.text);

if (this.fileWriter && isNodejs) fs.writeSync(this.fileWriter, unformattedText + '\n', null, 'utf-8');
if (this.fileWriter && (isNodejs || isElectronRenderer)) {
if (isElectronRenderer) {
unformattedText = unformattedText.replace(/\%c/gm, '');
}
fs.writeSync(this.fileWriter, unformattedText + '\n', null, 'utf-8');
}

if (isNodejs || !this.options.useColors) {
console.log(formattedText);
Expand Down
9 changes: 7 additions & 2 deletions example/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,18 @@ var Logger = function () {
}, {
key: '_write',
value: function _write(level, text) {
if ((this.options.filename || GlobalLogfile) && !this.fileWriter && isNodejs) this.fileWriter = fs.openSync(this.options.filename || GlobalLogfile, this.options.appendFile ? 'a+' : 'w+');
if ((this.options.filename || GlobalLogfile) && !this.fileWriter && (isNodejs || isElectronRenderer)) this.fileWriter = fs.openSync(this.options.filename || GlobalLogfile, this.options.appendFile ? 'a+' : 'w+');

var format = this._format(level, text);
var unformattedText = this._createLogMessage(level, text);
var formattedText = this._createLogMessage(level, text, format.timestamp, format.level, format.category, format.text);

if (this.fileWriter && isNodejs) fs.writeSync(this.fileWriter, unformattedText + '\n', null, 'utf-8');
if (this.fileWriter && (isNodejs || isElectronRenderer)) {
if (isElectronRenderer) {
unformattedText = unformattedText.replace(/\%c/gm, '');
}
fs.writeSync(this.fileWriter, unformattedText + '\n', null, 'utf-8');
}

if (isNodejs || !this.options.useColors) {
console.log(formattedText);
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ class Logger {
}

_write(level, text) {
if((this.options.filename || GlobalLogfile) && !this.fileWriter && isNodejs)
if((this.options.filename || GlobalLogfile) && !this.fileWriter && (isNodejs || isElectronRenderer))
this.fileWriter = fs.openSync(this.options.filename || GlobalLogfile, this.options.appendFile ? 'a+' : 'w+');

let format = this._format(level, text);
let unformattedText = this._createLogMessage(level, text);
let formattedText = this._createLogMessage(level, text, format.timestamp, format.level, format.category, format.text);

if(this.fileWriter && isNodejs)
if(this.fileWriter && (isNodejs || isElectronRenderer)) {
if (isElectronRenderer) { unformattedText = unformattedText.replace(/\%c/gm, ''); }
fs.writeSync(this.fileWriter, unformattedText + '\n', null, 'utf-8');
}

if(isNodejs || !this.options.useColors) {
console.log(formattedText)
Expand Down
11 changes: 11 additions & 0 deletions test/logplease.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,17 @@ describe('logplease', function() {
done()
})

it('writes to log file when in electron renderer process', (done) => {
process.type = 'renderer';
const log = Logger.create('test1', { filename: logfile, appendFile: false, showTimestamp: false })
assert(!fs.existsSync(logfile))
log.debug('hello')
assert(fs.existsSync(logfile))
const f = fs.readFileSync(logfile, 'utf-8')
assert.equal(f, '[DEBUG] test1: hello\n')
done()
});

it('appends to a log file', (done) => {
const log = Logger.create('test1', { filename: logfile, appendFile: true, showTimestamp: false })
assert(!fs.existsSync(logfile))
Expand Down