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

Error logging #263

Merged
merged 13 commits into from
Aug 25, 2018
Merged

Error logging #263

merged 13 commits into from
Aug 25, 2018

Conversation

Stefan-Hanke
Copy link
Contributor

This PR starts at server-side error logging - #253. It allows to parameterize the server with a log fn that is subsequently called on events and moves that got rejected.

Actually, I don't know what returning an object fron a message handler does? Is this printed to console.log?

Checklist

  • Use a separate branch in your local repo (not master).
  • Test coverage is 100% (or you have a story for why it's ok).

@coveralls
Copy link

coveralls commented Aug 22, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling bcb63ef on Stefan-Hanke:error_logging into 510977f on google:master.

Copy link
Member

@nicolodavis nicolodavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you sync this with 1688639? That commit moves this code to src/master/master.js.

@@ -112,10 +122,14 @@ export function Server({ games, db, _clientInfo, _roomInfo }) {
// TODO: We currently attach the log back into the state
// object before storing it, but this should probably
// sit in a different part of the database eventually.
log = [...log, ...state.deltalog];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the name clash with log here is problematic?

@@ -18,7 +18,7 @@ import { createApiServer, isActionFromAuthenticPlayer } from './api';
const PING_TIMEOUT = 20 * 1e3;
const PING_INTERVAL = 10 * 1e3;

export function Server({ games, db, _clientInfo, _roomInfo }) {
export function Server({ games, db, _clientInfo, _roomInfo, log }) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of parameterizing Server (which will get messy now that you have to pass this down many layers after #251), how about we just expose a logging module that is imported here and used? I don't actually imagine that people will want anything other than console.log, so instead of allowing a custom logger implementation, let's just turn it off in production builds?

Also, I think different logging levels make sense, which is easy to do if we just add two functions in the logging module:

// turned off in prod builds
// used for general diagnostic messages
logging.info()

// always on
// signals an unexpected value or other error
logging.error()

@@ -0,0 +1,16 @@
// evaluation of DEV needs to be done inside the functions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason that spying doesn't work? You can also try:

jest.spyOn(console, log)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the import is evaluated it is fixed on either production or not, but I want to test both.

// evaluation of DEV needs to be done inside the functions
// otherwise console.log cannot be changed to a spy function anymore.

export function info(msg) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think checking against these variables at each log call will eventually slow things down once we have a lot of logging.
Can you try the spy suggestion above and change this to something along the lines of:

let info = () => {};
let error = () => {};

if (DEV) {
  info = (msg) => console.log(...);
  error = (msg) => console.log(...);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK maybe that comment is misleading. What is meant is that once the module is imported, NODE_ENV is evaluted and the calls are either routed to console.log or not. Once fixed to console.log, it cannot be altered by a test anymore (in the sense that it is NOT called whatsoever) since that would require "running" the module again, evaluating NODE_ENV again after the test changed it.

My intention is to have tests that actually check that console.log is called when NODE_ENV==="test"|"development", and NOT called otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can accomplish this by some combination of jest.resetModules() and a require() statement to import the module (don't use the ES5 module import syntax).

Copy link
Member

@nicolodavis nicolodavis Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeEach(() => {
  jest.resetModules();   // this may or may not be actually necessary.
});

describe('dev', () => {
  process.env.NODE_ENV = 'development';
  const logger = require('./logger');
  ...
});

describe('prod', () => {
  process.env.NODE_ENV = 'production';
  const logger = require('./logger');
  ...
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using two separate test files is also an option if this doesn't work.

@nicolodavis
Copy link
Member

I inlined master.log.test.js in master.test.js. Having an extra test file that does the same setup work as master.test.js is not worth the additional slowdown of running the entire test suite on the CI.

@nicolodavis nicolodavis merged commit d60dd8a into boardgameio:master Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants