You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been playing around with 5.0.0-beta.25 and found very little about customizing the output in this version. After going through the source, I have created a more verbose example to help others.
The only strange thing is that your toJSON() method in electron-log/src/main/transforms/object.js returns an object, rather than JSON. It removes cyclic references so that JSON.stringify() can safely handle them, but the method name should probably be changed.
Renderer
This is my logger.js file. It initializes the log and keeps the format simple. Most importantly with this setup, the output from console.log(), console.error() etc. in the renderer process appear in both DevTools and the terminal output:
importlogfrom'electron-log/renderer';log.transports.console.format='{text}';// Echo console.log() etc. to the terminal using electron logger.Object.assign(console,log.functions);export{log};
Main Process
The log customization in the main process is more verbose, but simple to customize.
global.logHome and global.logLevel need to be set first:
constchalk=require('chalk');constlog=require('electron-log/main');const{ maxDepth, toJSON }=require('electron-log/src/main/transforms/object');asyncfunctioninitLogs(){constcolor={error: chalk.bgRed.white.bold,warn: chalk.yellow.bold,info: chalk.blue.bold,verbose: (t)=>t,debug: (t)=>t,silly: (t)=>t,};log.transports.file.resolvePathFn=()=>path.join(global.logHome,'debug_last.log');log.transports.file.level=global.logLevel;// Change the console output to just the text we create in our hook.log.transports.console.format='{text}';log.hooks.push((message,transport)=>{lettext=null;if(transport!==log.transports.console){returnmessage;}// Clone message and data because they are shared by the different// transports.constnewMessage=Object.assign({},message);const{ data, date, level }=newMessage;constdataClone=[...data];if(typeofdataClone[0]==='string'){text=dataClone[0];}else{// Deal with objects, arrays etc.// Step 1: Ensure the object is not deeper the 6 levels.letsafeObj=maxDepth({data: dataClone[0]});// Step 2: This 'toJSON' method actually removes cyclic references so that// JSON.stringify() can safely handle them.safeObj=toJSON({data: safeObj});// Step 3: JSON.stringify() the safe objecttext=JSON.stringify(toJSON({data: safeObj}));}// Personal tweak to highlight messages starting with 'XXXXX'if(text.startsWith('XXXXX')){text=chalk.bold(text);}// Build strings ready for outputconstcolorize=color[level];constlvl=('['+level+']').padStart(9,' ');constformattedTime=date.toTimeString().substring(0,8);// Tag entries with their process type:// - M: main// - R: rendererconstprocessType=newMessage.variables.processType==='main' ? 'M' : 'R';// Colorize the beginning of the outputconstprefix=colorize(`${formattedTime}${processType}${lvl}`);// Add the final string back to the clone of the data array and save it to// newMessage.datadataClone[0]=`${prefix}${text}`;newMessage.data=dataClone;// Return the newly constructed messagereturnnewMessage;});log.initialize({preload: true});Object.assign(console,log.functions);}
The text was updated successfully, but these errors were encountered:
I agree, it makes sense to make an example of output processing. In v5 a new property transport.transforms was introduced for that. I have added it to type definitions. But I want to play a bit more with it and add the corresponding documentation when I'm sure the public interface is good enough.
As for toJSON function, it just follows the standard convention when it returns a JSON representation of some object, it doesn't have to be a string.
I've been playing around with
5.0.0-beta.25
and found very little about customizing the output in this version. After going through the source, I have created a more verbose example to help others.The only strange thing is that your
toJSON()
method inelectron-log/src/main/transforms/object.js
returns an object, rather than JSON. It removes cyclic references so thatJSON.stringify()
can safely handle them, but the method name should probably be changed.Renderer
This is my
logger.js
file. It initializes the log and keeps the format simple. Most importantly with this setup, the output fromconsole.log()
,console.error()
etc. in the renderer process appear in both DevTools and the terminal output:Main Process
The log customization in the main process is more verbose, but simple to customize.
global.logHome
andglobal.logLevel
need to be set first:The text was updated successfully, but these errors were encountered: