megahertz / electron-log Goto Github PK
View Code? Open in Web Editor NEWSimple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
License: MIT License
Simple logging module Electron/Node.js/NW.js application. No dependencies. No complicated configuration.
License: MIT License
Hello,
if i disable the console transport and only allow the transport to file the file transport is also not working. I have located the "bug" in the log method of your library.
var transports = module.exports.transports;
for (var i in transports) {
// jshint -W089
if (!transports.hasOwnProperty(i) || typeof transports[i] !== 'function') {
return;
}
if (!compareLevels(transports[i].level, level)) {
return;
}
transports[i].call(module.exports, msg);
}
This is the part where the logic error lays. If the first transport is not a function the for loop will be canceled because of the return statement. And if i disable the console transport the typeof the console transport is boolean. So nothing will be logged.
My 'patch' is the following code:
var transports = module.exports.transports;
for (var i in transports) {
// jshint -W089
if (transports.hasOwnProperty(i) && typeof transports[i] == 'function') {
if (!compareLevels(transports[i].level, level)) {
return;
}
transports[i].call(module.exports, msg);
}
}
Hello.
I'm having few issues in the project (Angular and electron) implementing electron-log.
In the main.ts for electron i'm bringing in electron-log with:
import logger = require('electron-log');
transport is configured in the same main.ts file
In the renderer project I import electron-log with:
import log from 'electron-log';
The log messages appear in the console as they should from all components,
however, it only writes to a file from main.ts
The warnings I am getting:
electron-log: unable to load the app name from package.json
electron-log.transports.file: Could not set a log file
I had tried to use import log from 'electron-log/renderer'; instead but it didn't help and
also shows - Cannot read property 'info' of null error.
Am I missing something? How can this be fixed please.
Hi, thanks for the nice lib!
One quick request, could you create tag when you release new version, so that we can easily check diff between releases?
error
, warn
, info
and debug
levels should be translated to console.error(msg)
(etc) in the console transport; e.g.
log.transports.console = msg => {
if (console[msg.level]) {
console[msg.level](msg.text)
} else {
console.log(`[${msg.level}]`, msg.text)
}
}
I'm trying out electron-log and it looks great.
It seems (on Windows at least) that output goes to the terminal/shell used to launch my (electron) app rather than the Chromium Developer Tools Console inside the app.
Is this something that can be configured?
Can you clarify if we are to use log.warn() or log.warning()?
The LEVELS in index.js only has 'warn'.
[line 9] var LEVELS = [ 'error', 'warn', 'info', 'verbose', 'debug', 'silly' ];
But then in the examples and later in the code, you use 'warning'. EG:
[line 59] module.exports.transports.file.level = 'warning';
[line 125] log('warning', 'electron-log.transports.file: Could not set a log file');
I don't see how it would possibly pass the check in
[line 100] var pass = LEVELS.indexOf(passLevel);
I'm using log.warn(), but there seems to be a bug in the internal usage.
Per Electron's documentation:
You should usually also specify a productName field, which is your application's full capitalized name, and which will be preferred over name by Electron.
Electron's userData path utilizes the value returned from app.getName()
, so with this module's default of using name, when packageName exists, there would end up being two data directories for the app.
Node js and file transport is working well. but some how renderrer console logs are not getting visible Does the console log get visible by default or do we have to perform some configuration. An simple example of electron would be helpful.
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
const fs = require('fs');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({ width: 800, height: 600 })
// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
mainWindow.webContents.openDevTools()
var log = require('electron-log');
log.info('in create window');
log.transports.rendererConsole.level = 'info';
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
Hi
using:
electron-log@^2.2.9:
version "2.2.9"
resolved "https://registry.yarnpkg.com/electron-log/-/electron-log-2.2.9.tgz#e0484cb1a8a84593095e3b69f47361ae15d73bdf"
it seems something has changed in the electron-log API, or I have some really weird issue.
The docs say we should do this:
const log = require('electron-log');
log.transports.file.level = process.env.JBM_LOG_LEVEL || 'info';
log.transports.console.level = process.env.JBM_LOG_LEVEL || 'info';
But there is no transports
on the log object. Am I missing something?
node_modules/electron-log/electron-log.d.ts(52,16): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
I'm using the new renderer console transport and finding that the error information sent to log.error()
gets stripped out. For example:
log.error(new Error('This is an error Object'));
log.error('This is an error');
log.error('This is an error message', new Error('This is an error Object'));
Gives the following output:
{ data: [ {} ], date: 2017-04-27T00:34:01.286Z, level: 'error' }
{ data: [ 'This is an error' ], date: 2017-04-27T00:34:01.287Z, level: 'error' }
{ data: [ 'This is an error message', {} ], date: 2017-04-27T00:34:01.287Z,level: 'error' }
When I directly pass msg
to console.log()
like so:
log.transports.rendererConsole.format = (msg) => {
console.log(msg);
};
As you can see the Error
object turns into an empty object {}
. Any ideas what is going on here?
Ideally data
would contain the original Error
object so the error message and stack trace can be displayed.
I'm using v2.2.2.
Hey there, great package - thanks! ๐
We would like to be able to configure the paths for the various OSes where the log files are created because there are other processes logging stuff at already defined locations (e.g: in a Logs
subfolder of the current electron-log locations)
Hi
I am using electron-log in an electron app running on Windows. I am also using webpack.
The problem is that the log file gets created, 2 lines are written to it when the app starts, but then I see in my console "electron-log.transports.file: Could not set a log file"
Any idea why this happens? The log file is there, it just stops writing to it.
Thanks!
When using electron-log as a dependency in a renderer app to be served from a non-file URL, it is not possible to exempt it from webpack's bundling (using: externals: { 'electron-log': 'commonjs electron-log'}). However, the dynamic require in the loadAppPackage function complicates the bundling a lot, requiring one to use "exprContextRegExp: /$^/, exprContextCritical: false" in the webpack config.
When setting the appName property, the whole function becomes redundant - in the findLogPath function there is a line: "appName = appName || findAppName()", so loadAppPackage() is never called when the appName property is set.
Could we get electron-log to be more webpack friendly in this respect? I'd figure that pulling the offending code out, possibly doing something like a require('./findAppName') would simplify things for webpack, which could then use some ignore or noop plugin or whatever to patch the function.
Electron: 1.7.9
Nodejs: 9.2.0
OS: Windows 10
I have installed electron-log
npm install --save electron-log
I have included the module
var log = require('electron-log');
Yet upon attempting to log something
log.info('Hello, log - info');
log.warn('Hello, log - warning');
Absolutely nothing is happening. No errors are occuring, no log messages in console, and no file is being created where the documentation suggests it should be created:
%USERPROFILE%\AppData\Roaming\<app name>\log.log
Are you able to confirm this is working in node 9.2.0?
This works great in development, but when I build a release it fails as shown below.
Electron 1.2.3
"electron-prebuilt": "^1.2.3",
const log = require('electron-log');
log.debug('synthDefsDir', '/some/path');
Uncaught Exception:
RangeError: Maximum call stack size exceeded
at defineProperty (native)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:66:1)
at e (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:181:1)
at s (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:138:1)
at Object.u (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:110:1)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:87:1)
at e (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:181:1)
at s (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:138:1)
at Object.u (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:110:1)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:87:1)
at e (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:181:1)
at s (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:138:1)
at Object.u (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:110:1)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:87:1)
at e (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:181:1)
at s (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:138:1)
at Object.u (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:110:1)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:87:1)
at e (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:181:1)
at s (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:138:1)
at Object.u (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:110:1)
at r (/Users/crucial/code/idmx/playsplom/release/darwin-x64/PlaySPLOM-darwin-x64/PlaySPLOM.app/Contents/Resources/app/webpack:/~/electron-log/index.js:87:1)
Is there any way to change the location of the file after the first log method call?
Hi,
I saw you just recently enabled logging in the developer tools console, thanks for that.
Do you think it would be possible to be able to see from which line the log was emitted ? This is the console.log() behavior, and is very useful during development, especially to find error sources.
Currently, all log produced is showing as origin the js file of the module.
Thanks.
If you set the rendererConsole.level to a more verbose level than the console.level, all the messages from the rendererConsole leak into the console.
Example:
log.transports.console.level = 'verbose';
log.transports.rendererConsole.level = 'debug';
I've tried it on a Mac and it works like a charm but on a Windows 10, 32bits machine it doesn't create the log file. Any idea why?
Thanks!
In my main.js for electron i'm bringing in electron log with:
import log from 'electron-log';
I configure the transport formats in main.js.
I log a message from main.js and it appears in the log file and the console with the correct format.
When I log from my react application, it only shows up in the console with the default format.
Am I missing something in my config?
Would be really nice if log rotation or at least log file limit was supported.
I am looking to this library as the developer tools in a packed/production app don't show console logs (when they do during development).
I would like to log console.log from the renderer to both dev console and a file.
Is there a way to override console.log to achieve this? Also should this configuration only be done inside the main script, since no configuration can take place in the renderer if I read correctly?
Hi,
We've been using eletron-log but we sometimes get the following error:
Could not rotate log Error: ENOENT: no such file or directory, rename 'c:<app-dir-here>\log.log' -> 'c:<app-dir-here>\log.old.log'
An idea what it could be please?
Mark
Why app.getName is not used to get app name? To package.json loading.
Hi
log.transport.file = false;
log.transport.console = false;
should be:
log.transports.file = false;
log.transports.console = false;
thanx!
The {ms}
formatting expands to pad(date.getMilliseconds(), 4)
. Four digit subseconds is not milliseconds.
I'm not sure how log file is maintained after getting pretty big. Is it trimmed automatically? If no then could you add some option to trim the old logs? It's a corner case.
App logs to console, but I don't see any file at the default path ~/Library/Logs/<app name>/log.log
. I've checked both ~/Library/Logs/
and ~/Users/username/Library/Logs/
.
Please advise.
const log = require('electron-log')
log.transports.file.format = '{h}:{i}:{s}:{ms} {text}'
log.transports.file.maxSize = 5 * 1024 * 1024
function scheduleRandomNote () {
let rule
return schedule.scheduleJob(rule, function () {
log.info('triggered-random-note')
mainWindow.webContents.send('trigger-random-note')
})
}
Hi,
I'm doing some tests and I limited the file size to 100ko :
log.transports.file.maxSize = 1024 * 100;
On macOS the size is "respected".
It's always a little bit more but it comes from the way you do your check.
You check the file size is under the limit before to write so it's always a little bit more.
Fine.
But on windows the log rotate is done only when the stream starts.
So I start my electron app, its logs and I have a file size of +3000ko.
I restart my app, the log files is renames .old.log.
Is it the expected behavior ?
I have made a background process using hidden window in electron. In that process console.log() or electron-log dosn't print anywhere. What should I do. Also is there a way to redirect every output of console.log to electron-log?
Just FYI, in case you find this useful or interesting, I split out some common utilities around finding application logging directories, made it a bit more robust and made it async: https://github.com/JoshuaGross/app-log-dir
This isn't a feature request but it would simplify the core logic of electron-log
to rely on this library, if you like! I've given you proper attribution in that repo as well. Cheers!
My entry point is main/main.js
Hi Alex,
I'm trying to print a message in a main process. I've started with a very (very) basic sample - cloned https://github.com/electron/electron-quick-start
, put console.log and run electron .
- nothing showing.
Then I installed your module, added log.warn
and run electron .
- same result, no console output (besides file output is present).
Am I doing something completely wrong, or may it's not possible at all to show console logs from main.js
on Windows?
My environment:
electron 1.6.11
node 6.10.1
windows server 2012R2 (can be considered as Windows 10)
Hi, i'm using electron-log with electron-builder (specifically https://github.com/chentsulin/electron-react-boilerplate). The logs get written perfectly when i'm running it in dev mode but when i package and run it the logs don't seem to be written anywhere. Is there any recommended way to debug this?
One thing to note the app is running from the /Applications folder on my mac
Support for rotation of logs daily.
Something on the lines of -
https://github.com/winstonjs/winston-daily-rotate-file
Hi,
Am I missing something? or do these dependencies really are missing from package.json
electron
fs
and os
When the renderer process is called back with log message, it is using console to log message in the electron console
// in renderer.js line 25
...
module.exports.default = module.exports;
ipcRenderer.on('__ELECTRON_LOG_RENDERER__', function(event, level, text) {
if (level === 'verbose') {
level = 'log';
} else if (level === 'silly') {
level = 'debug';
}
console[level](text);
});
This is working well until you try to route all console message to electron log with:
// IN RENDERER PROCESS
import log from 'electron-log'
console.log = log.verbose
console.error = log.error
console.warn = log.warn
...
This is causing an infinite loop with infinite logging!
One solution is just to backup the original console method and used them in the call back like below:
module.exports.default = module.exports;
console.log2 = console.log
console.debug2 = console.debug
console.info2 = console.info
console.warn2 = console.warn
console.error2 = console.error
ipcRenderer.on('__ELECTRON_LOG_RENDERER__', function(event, level, text) {
if (level === 'verbose') {
level = 'log';
} else if (level === 'silly') {
level = 'debug';
}
console[level + '2'](text);
});
}
Hope it helps,
Regards
Looks to be caused by Lines 89-94.
if (!transports.hasOwnProperty(i) || typeof transports[i] !== 'function') {
return;
}
if (!compareLevels(transports[i].level, level)) {
return;
}
These should probably be continue;
statements. I changed these locally to continue;
and I get the expected result (no console log, but info
level logging to file, as setup in my app config)
Hello.
In my project(use electron & vue), I configure the transport formats in main.js like this.
```
const logFormat = '{h}:{i}:{s}:{ms} {text}'
log.transports.console.format = logFormat
log.transports.console.format = (msg) => util.format.apply(util, msg.data);
const appDataDir = path.join(app.getPath('appData'), 'my-project')
log.transports.file.level = 'info'
log.transports.file.format = logFormat
log.transports.file.maxSize = 5 * 1024 * 1024
log.transports.file.file = path.join(appDataDir, 'log.log')
log.transports.file.streamConfig = { flags: 'w' }
log.transports.file.stream = fs.createWriteStream('log.log')
1. This works in dev mode, except the log file does not in the path of configuration(appDataDir/log.log), but the project folder in my desktop.
2.When I build my app with electron builder and run, it shows me an error of
"**Uncaught Exception:
Error: EACCES: permission denied, open 'log.log**'"
Am I wrong with something? How can this be fixed please.
import * as electronLog from "electron-log";
export class BaseService {
public log: typeof electronLog;
constructor() {
const logPath = __dirname + "/logs/log.txt";
this.log = electronLog;
this.log.transports.console.level = false;
this.log.transports.file.level = "silly";
this.log.transports.file.format = "{y}-{m}-{d} {h}:{i}:{s}:{ms} {text}";
this.log.transports.file.maxSize = 5 * 1024 * 1024;
this.log.transports.file.file = logPath;
this.log.transports.file.streamConfig = { flags: "w" };
this.log.transports.file["stream"] = fs.createWriteStream(logPath);
this.log.transports.file.appName = "gamebrowser";
}
}
How can I use the existing log file without electron-log deleting it's contents?
Sorry this is probably super dumb questions but how do I force it to write logs not only for log.error/warn but also for log.info?
I tried something like this but no luck.
var log = require('electron-log')
log.transports.console.level = 'info'
import { log } from 'electron-log';
has no exported member 'log'.
Cant get this to work in typescript.
Attempting to log throws an exception on windows 10, as 'HOME' is not set in env.
Path.join fails here, as process.env['HOME'] is undefined:
https://github.com/megahertz/electron-log/blob/master/index.js#L180
I couldn't find any references in the source code... like an enum object or symbol map with some commments on the possible values. I assume these stem from the possible flags you can pass into a fs
digging around api documents i found this link
https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback
is it safe to assume you can pass any one of these into the streamConfig?
here is a brief overview
Asynchronous file open. See open(2). flags can be:
'r' - Open file for reading. An exception occurs if the file does not exist.
'r+' - Open file for reading and writing. An exception occurs if the file does not exist.
'rs+' - Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.
This is primarily useful for opening files on NFS mounts as it allows skipping the potentially stale local cache. It has a very real impact on I/O performance so using this flag is not recommended unless it is needed.
Note that this doesn't turn fs.open() into a synchronous blocking call. If synchronous operation is desired fs.openSync() should be used.
'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
'wx' - Like 'w' but fails if path exists.
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
'wx+' - Like 'w+' but fails if path exists.
'a' - Open file for appending. The file is created if it does not exist.
'ax' - Like 'a' but fails if path exists.
'a+' - Open file for reading and appending. The file is created if it does not exist.
'ax+' - Like 'a+' but fails if path exists.
mode sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0o666, readable and writable.
The callback gets two arguments (err, fd).
The exclusive flag 'x' (O_EXCL flag in open(2)) ensures that path is newly created. On POSIX systems, path is considered to exist even if it is a symlink to a non-existent file. The exclusive flag may or may not work with network file systems.
flags can also be a number as documented by open(2); commonly used constants are available from fs.constants. On Windows, flags are translated to their equivalent ones where applicable, e.g. O_WRONLY to FILE_GENERIC_WRITE, or O_EXCL|O_CREAT to CREATE_NEW, as accepted by CreateFileW.
On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
Note: The behavior of fs.open() is platform-specific for some flags. As such, opening a directory on macOS and Linux with the 'a+' flag - see example below - will return an error. In contrast, on Windows and FreeBSD, a file descriptor will be returned.
Maybe creating a config object to store these values could be helpful? Thanx :)
When app started with --inspect
, due to electron/electron#10260, console.debug
is defined but does nothing. Therefore, the console transport does not output debug level.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.