You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

log.js.flow 970B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // @flow
  2. import {
  3. boolean,
  4. } from 'boolean';
  5. import environmentIsNode from 'detect-node';
  6. import createGlobalThis from 'globalthis';
  7. import {
  8. createLogger,
  9. createMockLogger,
  10. createRoarrInititialGlobalState,
  11. } from './factories';
  12. const globalThis = createGlobalThis();
  13. const ROARR = globalThis.ROARR = createRoarrInititialGlobalState(globalThis.ROARR || {});
  14. let logFactory = createLogger;
  15. if (environmentIsNode) {
  16. // eslint-disable-next-line no-process-env
  17. const enabled = boolean(process.env.ROARR_LOG || '');
  18. if (!enabled) {
  19. logFactory = createMockLogger;
  20. }
  21. }
  22. export type {
  23. LoggerType,
  24. MessageType,
  25. TranslateMessageFunctionType,
  26. } from './types';
  27. export {
  28. ROARR,
  29. };
  30. export default logFactory((message) => {
  31. if (ROARR.write) {
  32. // Stringify message as soon as it is received to prevent
  33. // properties of the context from being modified by reference.
  34. const body = JSON.stringify(message);
  35. ROARR.write(body);
  36. }
  37. });