GithubHelp home page GithubHelp logo

sindresorhus / electron-timber Goto Github PK

View Code? Open in Web Editor NEW
387.0 9.0 13.0 75 KB

Pretty logger for Electron apps

License: MIT License

JavaScript 99.23% HTML 0.77%
electron-module electron logger logging-library logging nodejs npm-package

electron-timber's Introduction

electron-timber

Pretty logger for Electron apps

By default, logs from the renderer process don't show up in the terminal. Now they do.

You can use this module directly in both the main and renderer process.

Install

$ npm install electron-timber

Requires Electron 5 or later.

Usage

Main process:

const {app, BrowserWindow} = require('electron');
const logger = require('electron-timber');

let mainWindow;

(async () => {
	await app.whenReady();

	mainWindow = new BrowserWindow();
	await mainWindow.loadURL();

	logger.log('Main log');
	logger.error('Main error');

	const customLogger = logger.create({name: 'custom'});
	customLogger.log('Custom log');
})();

Renderer process:

const logger = require('electron-timber');

logger.log('Renderer log');
logger.error('Renderer error');

API

logger

Logging will be prefixed with either main or renderer depending on where it comes from.

Logs from the renderer process only show up if you have required electron-timber in the main process.

The methods are bound to the class instance, so you can do: const log = logger.log; log('Foo');.

log(…values)

Like console.log.

warn(…values)

Like console.warn.

error(…values)

Like console.error.

time(label)

Like console.time.

timeEnd(label)

Like console.timeEnd.

streamLog(stream)

Log each line in a stream.Readable. For example, child_process.spawn(…).stdout.

streamWarn(stream)

Same as streamLog, but logs using console.warn instead.

streamError(stream)

Same as streamLog, but logs using console.error instead.

create([options])

Create a custom logger instance.

You should initialize this on module load so prefix padding is consistent with the other loggers.

options

Type: object

name

Type: string

Name of the logger. Used to prefix the log output. Don't use main or renderer.

ignore

Type RegExp

Ignore lines matching the given regex.

logLevel

Type: string

Can be info (log everything), warn (log warnings and errors), or error (log errors only). Defaults to info during development and warn in production.

getDefaults()

Gets the default options (across main and renderer processes).

setDefaults([options]) Main process only

Sets the default options (across main and renderer processes).

options

Type: object

Same as the options for create().

Toggle loggers

You can show the output of only a subset of the loggers using the environment variable TIMBER_LOGGERS. Here we show the output of the default renderer logger and a custom unicorn logger, but not the default main logger:

TIMBER_LOGGERS=renderer,unicorn electron .

Related

Maintainers

License

MIT

electron-timber's People

Contributors

acheronfail avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

electron-timber's Issues

Separate main and renderer process logic

As discussed in #10 - the codebase may be much easier to maintain if we separated the main and renderer logic into separate files. This issue will track the progress of this.

Current goals:

  • Separate main and renderer process logic
  • Improve tests
    • Make it easier to write tests without them effecting each other
    • Hopefully make it pleasant to write tests (we can only go so far with Spectron)

Weird character on the log

Sorry not sure if this is an actual problem, or just something missing some configuration on my end, or ConEmu making things not work...

On my console I see the outputs like:
main ÔÇ║ ####################
main ÔÇ║ #### NEW START #####
main ÔÇ║ ###################

Meaning that I get ÔÇ║ instead of the >

Also the output is not in colors.

What could be causing this problems?
Thanks in advance for the support.

Toggle logger output with environment variables

Just an idea but it would be cool if you could control the output with environment variables.

Something like:

$ TIMBER_LOGGERS="main,render,custom" yarn run dev

and if you wanted to disable custom due to excessive noise you could launch with:

$ TIMBER_LOGGERS="main,render" yarn run dev

That way you could enable/disable noisy loggers without needing code modifications.

Thoughts?

Add ability to also log to disk

We can use app.getPath('logs') to get the path to the logs directory.

The person that implements this should do some research on how other log solutions do it. Regarding max size, log rotation, etc.

Add support for timestamp

Currently the output looks like:

main.js > Log

i would love to see support for leading timestamps. Example

22:10:34 main.js > Log

Add `logLevel` option

I'm thinking it might be useful to have a logLevel option, so you could choose what level to log based on the environment. I think a good default would be to set it to log (info) in development and warn in production, so it doesn't output log messages in production.

There should also be a way to set it for all logger instances, maybe:

const logger = require('timber');

logger.setDefaults({
	logLevel: 'warn'
});

@acheronfail @lukechilds Thoughts?

Please add TypeScript definitions

I think a lot of people, maybe even most people, are going to start new Electron projects in 2021 using TypeScript.

Please add a d.ts file to this repository that contains the definitions for the API, so that TypeScript can work with this library.

electron-builder redistributables can't find preload.js

I noticed that my electron program fails to start because it can't find preload.js. Looking at the webpack-compiled bundle, I found that the culprit must be electron-timber, since it's the only module that looks for preview.js. Here's the output:

[26945:0728/234248.108082:ERROR:CONSOLE(164)] "Unable to load preload script: /home//projects/electron/nuclear/release/linux-unpacked/resources/app.asar/dist/preload.js", source: /home//projects/electron/nuclear/release/linux-unpacked/resources/electron.asar/renderer/init.js (164)
[26945:0728/234248.108149:ERROR:CONSOLE(165)] "Error: Cannot find module '/home//projects/electron/nuclear/release/linux-unpacked/resources/app.asar/dist/preload.js'
    at Module._resolveFilename (module.js:543:15)
    at Function.Module._resolveFilename (/home//projects/electron/nuclear/release/linux-unpacked/resources/electron.asar/common/reset-search-paths.js:35:12)
    at Function.Module._load (module.js:473:25)
    at Module.require (module.js:586:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home//projects/electron/nuclear/release/linux-unpacked/resources/electron.asar/renderer/init.js:162:5)
    at Object.<anonymous> (/home//projects/electron/nuclear/release/linux-unpacked/resources/electron.asar/renderer/init.js:188:3)
    at Module._compile (module.js:642:30)
    at Object.Module._extensions..js (module.js:653:10)
    at Module.load (module.js:561:32)", source: /home//projects/electron/nuclear/release/linux-unpacked/resources/electron.asar/renderer/init.js (165)

It loads fine in debug build. What prevents it from running in production build?

Opt-in way to hook `console` methods

Some people may want to direct all logging to electron-timber, even logging they don't control. The only solution then is to hook the console.* methods. I think we should provide a way to easily do that.

Something like this:

logger.setDefaults({hookConsole: true});

And you can then use console.log in the renderer and have it appear in the terminal.

@acheronfail Thoughts?

Filter out Electron junk logging

See electron/electron#12438 (comment)

Electron logs a lot of annoying stuff and it doesn't seem like it will be fixed upstream anytime soon.

Here's what I get in the app I'm working on:

2018-05-07 02:20:07.532 Electron[5632:177578] *** WARNING: Textured window <AtomNSWindow: 0x7f951ec62830> is getting an implicitly transparent titlebar. This will break when linking against newer SDKs. Use NSWindow's -titlebarAppearsTransparent=YES instead.
2018-05-07 02:20:08.440 Electron Helper[5635:177698] Couldn't set selectedTextBackgroundColor from default ()
[5632:0507/022008.491482:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)
[5632:0507/022009.415028:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)
[5632:0507/022010.414003:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)
[5632:0507/022011.413684:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)
[5632:0507/022012.415183:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)
[5632:0507/022013.415071:ERROR:CONSOLE(7574)] "Extension server error: Operation failed: : has no execution context", source: chrome-devtools://devtools/bundled/inspector.js (7574)

Another error we should filter out:

[93943:0510/142524.901813:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)

No color showing up!

No color is showing up in VSCode Terminal when using
image
I'm using Cmder v1.3.19.1181, VSCode v1.64.2, Electron-Timber v0.5.1, Electron v17.0.0, Node v17.4.0, and last but not least Windows 11 (Using Unicode Beta)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.