Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose logger implemtation to modules for testing #63

Open
2 tasks done
Muhomorik opened this issue Oct 9, 2023 · 2 comments
Open
2 tasks done

Expose logger implemtation to modules for testing #63

Muhomorik opened this issue Oct 9, 2023 · 2 comments
Labels
enhancement New feature or request pr wanted The best way to get this implemented is to submit a PR!

Comments

@Muhomorik
Copy link

Is this a feature relevant to companion itself, and not a module?

  • I believe this to be a feature for companion, not a module

Is there an existing issue for this?

  • I have searched the existing issues

Describe the feature

Hello,

I would like to log errors in my logic that is not related to companion and main problem is that logger (winston) is defined in companion itself.

There are two different logs

Companion log, is OK, because I am in "module code" then, creating presets, actions, etc and have access to InstanceBase.

Logging Logging works because module should be either mocked or initialized for tests to work (infrastructure tests).

companion_log

Now, this is my module logs that I am talking about.

module_log

My logic, like API calls, are not by any way related to companion and do not require it (actually, don't want it in global tab).

Let's take example from Jest.

An ES6 Class Example

With logger, I want to abstract from console and write

import { logger } from '../../logger.js'

export default class SoundPlayer {
  constructor() {
    this.foo = 'bar';
  }

  playSoundFile(fileName) {
    logger.warn('Playing sound file' + fileName);
  }
}

And set log level once from module config in main.js

if (isDebug) {
    logger.level = 'debug'
} else {
    logger.level = 'error'
} 

This is problematic, because of logger dependency from module base.

I want to propose to declare logger (winston) "somewhere else" :) And let developers use it, instead of controller, will all functions and overloads.
When running as module, it should take default implementation with transports. Something like CompanionTransport and MyModuleTransport.

Basically, I want logger.js in my repo and use logger from there.

Probably, something like that
https://github.com/bitfocus/companion/blob/1b6dfc49a2816568f68f43bfe360d6bcd49817de/lib/Log/Controller.js#L95C13-L95C13

May be related:
bitfocus/companion#2420

LogController

Usecases

Development and debugging mostly.

@Julusian Julusian transferred this issue from bitfocus/companion Oct 9, 2023
@Julusian Julusian added the enhancement New feature or request label Oct 9, 2023
@Julusian
Copy link
Member

Julusian commented Oct 9, 2023

This is a little challenging to do because of the isolation used for the modules. Because there is no shared code or memory space, all communication is being done over IPC.
But it should be possible to expose a winston like logger object that could be used. Some care will need to be taken, as until the runEntrypoint() function in your main.js file is called, the logger object will send everything to a blackhole or the console, as ipc isnt setup by that point.

I think for this to happen it is going to require someone to make a PR, I am unlikely to do this myself. When I have wanted to do this, I have passed the main instance around, most likely typed as just the logging functions. While more effort, it works around this without being too messy.

The point in the code to setup the logger would be

.
As that is in the constructor of the InstanceBase class, you could also do this entirely just within your module and get the same behaviour (although it would be setup a little bit later, after upgrade scripts have been run)

@Julusian Julusian added the pr wanted The best way to get this implemented is to submit a PR! label Oct 9, 2023
@Muhomorik
Copy link
Author

Thank you for reply.
Yes, it can be hard to do with isolation and I am not sure if it is worth it.

What is the recommended way then?
I tried to add Winston to my module and it works, but only because it writes to console and companion threats it as logs.

Add custom transport like ToMemoryTransport and let it communicate to companion via IPC?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pr wanted The best way to get this implemented is to submit a PR!
Projects
None yet
Development

No branches or pull requests

2 participants