electron-log
Advanced tools
Comparing version 4.0.0-beta.1 to 4.0.0-beta.2
{ | ||
"name": "electron-log", | ||
"version": "4.0.0-beta.1", | ||
"version": "4.0.0-beta.2", | ||
"description": "Just a very simple logging module for your Electron application", | ||
@@ -37,5 +37,2 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"@babel/core": "*", | ||
"@babel/preset-env": "*", | ||
"babel-loader": "*", | ||
"electron": "*", | ||
@@ -42,0 +39,0 @@ "eslint": "^6.6.0", |
@@ -16,10 +16,14 @@ # electron-log | ||
- **on macOS:** `~/Library/Logs/{app name}/{process type}.log` | ||
- **on Windows:** `%USERPROFILE%\AppData\Roaming\{app name}\{process type}.log` | ||
- **on Windows:** `%USERPROFILE%\AppData\Roaming\{app name}\logs\{process type}.log` | ||
## Installation | ||
Install with [npm](https://npmjs.org/package/electron-log): | ||
Install with [npm](https://npmjs.org/package/electron-log) (v3): | ||
npm install electron-log | ||
or you can try v4-beta: | ||
npm install electron-log@beta | ||
## Usage | ||
@@ -66,8 +70,8 @@ | ||
- **[format](docs/format.md)**, default | ||
`'%c{h}:{i}:{s}.{ms}%c › {text}'` (main), | ||
`'{h}:{i}:{s}.{ms} › {text}'` (renderer) | ||
- **level**, default 'silly' | ||
- **forceStyles**, use styles in the main process even if TTY isn't attached, | ||
default false | ||
- **[format](docs/format.md)**, default | ||
`'%c{h}:{i}:{s}.{ms}%c › {text}'` (main), | ||
`'{h}:{i}:{s}.{ms} › {text}'` (renderer) | ||
- **level**, default 'silly' | ||
- **forceStyles**, use styles in the main process even if TTY isn't attached, | ||
default false | ||
@@ -80,7 +84,7 @@ #### File transport | ||
- **fileName**, default 'main.log' or 'renderer.log' | ||
- **[format](docs/format.md)**, default | ||
`'[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}'` | ||
- **level**, default 'silly' | ||
- **maxSize** of log file in bytes, 1048576 (1mb) by default. | ||
- **fileName**, default 'main.log' or 'renderer.log' | ||
- **[format](docs/format.md)**, default | ||
`'[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}'` | ||
- **level**, default 'silly' | ||
- **maxSize** of log file in bytes, 1048576 (1mb) by default. | ||
@@ -96,3 +100,3 @@ [Read more about file transport](docs/file.md). | ||
- **level**, default 'silly' | ||
- **level**, default 'silly' | ||
@@ -105,4 +109,4 @@ #### Remote transport | ||
- **level**, default false | ||
- **url**, remote endpoint | ||
- **level**, default false | ||
- **url**, remote endpoint | ||
@@ -124,3 +128,3 @@ [Read more about remote transport](docs/remote.md). | ||
easily override/add your own transport. | ||
[More info.](docs/extend.md#transport) | ||
[More info](docs/extend.md#transport). | ||
@@ -154,6 +158,6 @@ ### Colors | ||
- **showDialog**, default true for the main process. Set it to false to prevent | ||
showing a default electron error dialog | ||
- **onError**, `(error) => void | false`, default null - attach a custom | ||
error handler. If the handler returns false, this error will not be processed. | ||
- **showDialog**, default true for the main process. Set it to false to prevent | ||
showing a default electron error dialog | ||
- **onError**, `(error) => void | false`, default null - attach a custom | ||
error handler. If the handler returns false, this error will not be processed. | ||
@@ -167,3 +171,3 @@ ### [Hooks](docs/extend.md#hooks) | ||
[More info.](docs/extend.md#hooks) | ||
[More info](docs/extend.md#hooks). | ||
@@ -182,3 +186,3 @@ ### Multiple logger instances | ||
- [electron-cfg](https://github.com/megahertz/electron-cfg) - Just a simple | ||
solution for storing settings for an Electron application. | ||
- [electron-cfg](https://github.com/megahertz/electron-cfg) - | ||
Settings manager for your Electron application. |
@@ -22,3 +22,3 @@ 'use strict'; | ||
for (var i in transports) { | ||
if (transports.hasOwnProperty(i)) { | ||
if (Object.prototype.hasOwnProperty.call(transports, i)) { | ||
runTransport(transports[i], message, electronLog); | ||
@@ -25,0 +25,0 @@ } |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var format = require('../format'); | ||
var transform = require('../transform'); | ||
@@ -21,44 +21,45 @@ var original = { | ||
function consoleTransportFactory(electronLog) { | ||
var separator = process.platform === 'win32' ? '>' : '›'; | ||
var DEFAULT_FORMAT = { | ||
browser: '%c{h}:{i}:{s}.{ms}%c ' + separator + ' {text}', | ||
renderer: '{h}:{i}:{s}.{ms} › {text}', | ||
}; | ||
function consoleTransportFactory() { | ||
transport.level = 'silly'; | ||
transport.forceStyles = Boolean(process.env.FORCE_STYLES); | ||
transport.format = DEFAULT_FORMAT[process.type] || DEFAULT_FORMAT.browser; | ||
if (process.type === 'renderer') { | ||
transport.format = '{h}:{i}:{s}.{ms} › {text}'; | ||
} else { | ||
var separator = process.platform === 'win32' ? '>' : '›'; | ||
transport.format = '%c{h}:{i}:{s}.{ms}%c ' + separator + ' {text}'; | ||
} | ||
return transport; | ||
function transport(msg) { | ||
var text = format.format(msg, transport.format, electronLog); | ||
function transport(message) { | ||
if (process.type === 'renderer') { | ||
consoleLog(msg.level, [text].concat(msg.styles)); | ||
var content = transform.transform(message, [ | ||
transform.customFormatterFactory(transport.format), | ||
]); | ||
consoleLog(message.level, content); | ||
return; | ||
} | ||
var styles = msg.styles || []; | ||
var useStyles = transport.forceStyles || canUseStyles(message.level); | ||
if (transport.format.substr && transport.format.substr(0, 2) === '%c') { | ||
styles = ['color:' + levelToStyle(msg.level), 'color:unset'] | ||
.concat(styles); | ||
} | ||
var styledContent = transform.transform(message, [ | ||
addTemplateColorFactory(transport.format), | ||
transform.customFormatterFactory(transport.format), | ||
useStyles ? transform.applyAnsiStyles : transform.removeStyles, | ||
transform.toJSON, | ||
]); | ||
if (transport.forceStyles || canUseStyles(msg.level)) { | ||
consoleLog(msg.level, [applyAnsiStyles(text, styles)]); | ||
} else { | ||
consoleLog(msg.level, [text.replace(/%c/g, '')]); | ||
} | ||
consoleLog(message.level, styledContent); | ||
} | ||
} | ||
function applyAnsiStyles(text, styles) { | ||
styles.forEach(function (style) { | ||
text = text.replace('%c', cssToAnsi(style)); | ||
}); | ||
function addTemplateColorFactory(format) { | ||
return function addTemplateColors(data, message) { | ||
if (format !== DEFAULT_FORMAT.browser) { | ||
return data; | ||
} | ||
return text + '\x1b[0m'; | ||
return ['color:' + levelToStyle(message.level), 'color:unset'].concat(data); | ||
}; | ||
} | ||
@@ -80,26 +81,9 @@ | ||
function cssToAnsi(style) { | ||
var color = style.replace(/color:\s*(\w+).*/, '$1').toLowerCase(); | ||
switch (color) { | ||
case 'unset': return '\x1b[0m'; | ||
case 'black': return '\x1b[30m'; | ||
case 'red': return '\x1b[31m'; | ||
case 'green': return '\x1b[32m'; | ||
case 'yellow': return '\x1b[33m'; | ||
case 'blue': return '\x1b[34m'; | ||
case 'magenta': return '\x1b[35m'; | ||
case 'cyan': return '\x1b[36m'; | ||
case 'white': return '\x1b[37m'; | ||
default: return ''; | ||
} | ||
} | ||
function levelToStyle(level) { | ||
switch (level) { | ||
case 'error': return 'red'; | ||
case 'warn': return 'yellow'; | ||
case 'info': return 'cyan'; | ||
default: return 'unset'; | ||
case 'error': return 'red'; | ||
case 'warn': return 'yellow'; | ||
case 'info': return 'cyan'; | ||
default: return 'unset'; | ||
} | ||
} |
@@ -6,3 +6,3 @@ 'use strict'; | ||
var util = require('util'); | ||
var format = require('../../format'); | ||
var transform = require('../../transform'); | ||
var FileRegistry = require('./file').FileRegistry; | ||
@@ -44,4 +44,4 @@ var variables = require('./variables'); | ||
function transport(msg) { | ||
var file = getFile(msg); | ||
function transport(message) { | ||
var file = getFile(message); | ||
@@ -56,3 +56,9 @@ var needLogRotation = transport.maxSize > 0 | ||
file.writeLine(format.format(msg, transport.format, electronLog, true)); | ||
var content = transform.transform(message, [ | ||
transform.removeStyles, | ||
transform.customFormatterFactory(transport.format), | ||
transform.toString, | ||
]); | ||
file.writeLine(content); | ||
} | ||
@@ -59,0 +65,0 @@ |
@@ -17,3 +17,3 @@ 'use strict'; | ||
function readPackageJson() { | ||
return tryReadJsonAt(require.main.filename) | ||
return tryReadJsonAt(require.main && require.main.filename) | ||
|| tryReadJsonAt(process.resourcesPath, 'app.asar') | ||
@@ -20,0 +20,0 @@ || tryReadJsonAt(process.cwd()) |
'use strict'; | ||
var format = require('../format'); | ||
var transform = require('../transform'); | ||
var electronApi = require('../electronApi'); | ||
@@ -14,2 +14,4 @@ var log = require('../log.js'); | ||
electronApi.onIpc(transport.eventId, function (_, message) { | ||
message.date = new Date(message.date); | ||
log.runTransport( | ||
@@ -27,5 +29,11 @@ electronLog.transports.console, | ||
function transport(message) { | ||
message.data = message.data.map(format.stringifyObject); | ||
electronApi.sendIpc(transport.eventId, message); | ||
var ipcMessage = Object.assign({}, message, { | ||
data: transform.transform(message, [ | ||
transform.removeStyles, | ||
transform.toJSON, | ||
]), | ||
}); | ||
electronApi.sendIpc(transport.eventId, ipcMessage); | ||
} | ||
} |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var url = require('url'); | ||
var transform = require('../transform'); | ||
@@ -20,15 +21,17 @@ module.exports = remoteTransportFactory; | ||
function transport(msg) { | ||
function transport(message) { | ||
if (!transport.url) return; | ||
var data = jsonDepth({ | ||
post(transport.url, { | ||
client: transport.client, | ||
data: msg.data, | ||
date: msg.date.getTime(), | ||
level: msg.level, | ||
styles: msg.styles, | ||
variables: msg.variables, | ||
}, transport.depth + 1); | ||
post(transport.url, data); | ||
data: transform.transform(message, [ | ||
transform.removeStyles, | ||
transform.toJSON, | ||
transform.maxDepthFactory(transport.depth + 1), | ||
]), | ||
date: message.date.getTime(), | ||
level: message.level, | ||
styles: message.styles, | ||
variables: message.variables, | ||
}); | ||
} | ||
@@ -59,44 +62,1 @@ | ||
} | ||
function jsonDepth(json, depth) { | ||
if (depth < 1) { | ||
if (Array.isArray(json)) return '[array]'; | ||
if (typeof json === 'object') return '[object]'; | ||
return json; | ||
} | ||
if (Array.isArray(json)) { | ||
return json.map(function (child) { | ||
return jsonDepth(child, depth - 1); | ||
}); | ||
} | ||
if (json && typeof json.getMonth === 'function') { | ||
return json; | ||
} | ||
if (json === null) { | ||
return null; | ||
} | ||
if (typeof json === 'object') { | ||
if (json instanceof Error) { | ||
return json.stack || json.constructor.name + ': ' + json.message; | ||
} | ||
if (typeof json.toJSON === 'function') { | ||
json = json.toJSON(); | ||
} | ||
var newJson = {}; | ||
for (var i in json) { | ||
if (!Object.prototype.hasOwnProperty.call(json, i)) continue; | ||
newJson[i] = jsonDepth(json[i], depth - 1); | ||
} | ||
return newJson; | ||
} | ||
return json; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51126
9
20
1537
180
2