fgm / filog

A fine logger package for Meteor.JS applications (client + server)
GNU General Public License v3.0
18 stars 6 forks source link

How to use global.logger inside /imports folder #74

Closed didemkaraaslan closed 3 years ago

didemkaraaslan commented 4 years ago

So I have

client/main.js

import { ClientLogger, TrivialStrategy, MeteorClientMethodSender } from "filog";

Meteor.startup(() => {
    const sender = new MeteorClientMethodSender();
    const strategy = new TrivialStrategy(sender);
    window.logger = new ClientLogger(strategy);
}

server/main.js


import { Meteor } from "meteor/meteor";
import { WebApp } from "meteor/webapp";
import { TrivialStrategy, ServerLogger, ConsoleSender } from "filog";

Meteor.startup(() => {
  const sender = new ConsoleSender();
  const strategy = new TrivialStrategy(sender);
  global.logger = new ServerLogger(strategy, WebApp);
});`

My question is how do I use logger inside /imports/api/methods.js file?

imports/api/methods.js

global.logger.error("error"); // global.logger is undefined here..

fgm commented 4 years ago

You need to inject the logger as a dependency wherever you use it instead of pulling it from globals. You can write something like this:

# server/main.js
import { myFunctionNeedingALogger } from "api/methods";
const logger = global.logger = new ServerLogger(strategy, WebApp);
myFunctionNeedingALogger(logger);
# same for client/main.js

That way, the function used isomorphically in imports/api/methods receives the logger as a parameter depending on the environment.

fgm commented 3 years ago

No answer in over 1 year, assuming fixed.