-
Notifications
You must be signed in to change notification settings - Fork 710
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
Error logging #263
Conversation
There was a problem hiding this 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.
src/server/index.js
Outdated
@@ -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]; |
There was a problem hiding this comment.
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?
src/server/index.js
Outdated
@@ -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 }) { |
There was a problem hiding this comment.
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()
src/core/logger.js
Outdated
@@ -0,0 +1,16 @@ | |||
// evaluation of DEV needs to be done inside the functions |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
src/core/logger.js
Outdated
// 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) { |
There was a problem hiding this comment.
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(...);
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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');
...
});
There was a problem hiding this comment.
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.
I inlined |
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
master
).