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

Ability to enable logger in production #402

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/gasket-log/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"require": "@babel/register"
}
2 changes: 2 additions & 0 deletions packages/gasket-log/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# `@gasket/log`

- Support enabling logging in production

### 6.1.0

- Support for custom log levels ([#279])
Expand Down
9 changes: 9 additions & 0 deletions packages/gasket-log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class YourComponent extends React.Component {
}
```

The constructor accepts an object with the following optional properties:

| Property | Description |
|-------------|-------------|
| `level` | The maximum logging level to enable output for. Defaults to `info` |
| `levels` | Array of custom logging level names. |
| `namespace` | String for namespacing your logs. See [diagnostics] for more information. Your namespace is automatically prefixed with `gasket:` |
| `prod` | If set to `true` enables logging even for production builds. By default production builds have no client-side logging. |

> **NOTE:** The client logger uses [diagnostics] to output log messages to the
> console. Ensure one of the trigger mechanics for [diagnostics in browser] is
> set. The name used for diagnostics is `gasket*`.
Expand Down
2 changes: 1 addition & 1 deletion packages/gasket-log/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"posttest": "npm run lint",
"build": "babel ./src/client.js -d lib --delete-dir-on-start",
"prepublishOnly": "npm run build",
"test:client": "DEBUG=gasket:* mocha --require @babel/register test/client.test.js",
"test:client": "mocha test/client.test.js",
"test:server": "mocha test/server.test.js"
},
"repository": {
Expand Down
7 changes: 5 additions & 2 deletions packages/gasket-log/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ export default class Log {
* @param {Object} options configuration.
* @private
*/
constructor({ level, levels = Log.levels, namespace } = {}) {
constructor({ level, levels = Log.levels, namespace, prod } = {}) {
this.namespace = Array.isArray(namespace) ? namespace : [namespace];
this.level = ~levels.indexOf(level) ? level : 'info';

levels.forEach(lvl => {
this[lvl] = diagnostics(['gasket', lvl, ...this.namespace].filter(Boolean).join(':'));
this[lvl] = diagnostics(
['gasket', lvl, ...this.namespace].filter(Boolean).join(':'),
{ force: Boolean(prod) }
);
});
}

Expand Down
14 changes: 14 additions & 0 deletions packages/gasket-log/test/client.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-process-env */

import { describe, it } from 'mocha';
import { config } from 'winston';
import Log from '../src/client';
Expand All @@ -8,6 +10,8 @@ describe('Log', function () {
let log;

beforeEach(function () {
process.env.NODE_ENV = 'test';
process.env.DEBUG = 'gasket:*';
log = new Log();
});

Expand Down Expand Up @@ -55,6 +59,16 @@ describe('Log', function () {
assume(log).to.have.property('level', 'debug');
});

it('has an prod option to enable NODE_ENV=production logging', function () {
process.env.NODE_ENV = 'production';
const console = sinon.stub(global.console, 'log');

log = new Log({ namespace: 'uxcore2', prod: true });
log.info('something');

assume(console.callCount).equals(1);
});

describe('.log', function () {
it('is a function', function () {
assume(log.log).to.be.a('function');
Expand Down