Skip to content

Commit

Permalink
Merge 81d7dfd into abffed5
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed Nov 5, 2018
2 parents abffed5 + 81d7dfd commit 4f2f2dc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
37 changes: 37 additions & 0 deletions README.md
Expand Up @@ -187,6 +187,25 @@ Note that only the [JSON0 OT type](https://github.com/ottypes/json0) is supporte
`share.close(callback)`
Closes connections to the database and pub/sub adapters.

### Logging

ShareDB logs some errors and warnings, which can be useful for debugging. By default, the log level is set to `warn`. This can be configured through the `logLevel` constructor argument, which can be set to any of the following:

- `silent`
- `trace`
- `debug`
- `info`
- `warn` (default)
- `error`

For example, the following will disable anything except errors:

```js
var share = new Backend({
logLevel: 'error',
});
```

## Client API

The client API can be used from either Node or a browser. First, get a `ShareDB.Connection` object by connecting to the ShareDB server instance:
Expand Down Expand Up @@ -358,6 +377,24 @@ after a sequence of diffs are handled.
`query.on('extra', function() {...}))`
(Only fires on subscription queries) `query.extra` changed.

### Logging

ShareDB logs some errors and warnings, which can be useful for debugging. By default, the log level is set to `warn`. This can be configured through the `logLevel` constructor argument, which can be set to any of the following:

- `silent`
- `trace`
- `debug`
- `info`
- `warn` (default)
- `error`

For example, the following will disable everything except errors:

```js
var Client = require('sharedb/lib/client');
Client.setLogLevel('error');
```


## Error codes

Expand Down
9 changes: 5 additions & 4 deletions lib/agent.js
@@ -1,6 +1,7 @@
var hat = require('hat');
var util = require('./util');
var types = require('./types');
var logger = require('loglevel').getLogger('sharedb');

/**
* Agent deserializes the wire protocol messages received from the stream and
Expand Down Expand Up @@ -47,7 +48,7 @@ module.exports = Agent;
// Close the agent with the client.
Agent.prototype.close = function(err) {
if (err) {
console.warn('Agent closed due to error', this.clientId, err.stack || err);
logger.warn('Agent closed due to error', this.clientId, err.stack || err);
}
if (this.closed) return;
// This will end the writable stream and emit 'finish'
Expand Down Expand Up @@ -95,7 +96,7 @@ Agent.prototype._subscribeToStream = function(collection, id, stream) {
// Log then silently ignore errors in a subscription stream, since these
// may not be the client's fault, and they were not the result of a
// direct request by the client
console.error('Doc subscription stream error', collection, id, data.error);
logger.error('Doc subscription stream error', collection, id, data.error);
return;
}
if (agent._isOwnOp(collection, data)) return;
Expand Down Expand Up @@ -137,7 +138,7 @@ Agent.prototype._subscribeToQuery = function(emitter, queryId, collection, query
// Log then silently ignore errors in a subscription stream, since these
// may not be the client's fault, and they were not the result of a
// direct request by the client
console.error('Query subscription stream error', collection, query, err);
logger.error('Query subscription stream error', collection, query, err);
};

emitter.onOp = function(op) {
Expand Down Expand Up @@ -195,7 +196,7 @@ Agent.prototype._reply = function(request, err, message) {
};
} else {
if (err.stack) {
console.warn(err.stack);
logger.warn(err.stack);
}
request.error = {
code: err.code,
Expand Down
3 changes: 3 additions & 0 deletions lib/backend.js
Expand Up @@ -12,6 +12,7 @@ var Snapshot = require('./snapshot');
var StreamSocket = require('./stream-socket');
var SubmitRequest = require('./submit-request');
var types = require('./types');
var logger = require('loglevel').getLogger('sharedb');

var warnDeprecatedDoc = true;
var warnDeprecatedAfterSubmit = true;
Expand Down Expand Up @@ -48,6 +49,8 @@ function Backend(options) {
if (!options.disableSpaceDelimitedActions) {
this._shimAfterSubmit();
}

logger.setLevel(options.logLevel || logger.levels.WARN);
}
module.exports = Backend;
emitter.mixin(Backend);
Expand Down
14 changes: 9 additions & 5 deletions lib/client/connection.js
Expand Up @@ -5,6 +5,8 @@ var emitter = require('../emitter');
var ShareDBError = require('../error');
var types = require('../types');
var util = require('../util');
// TODO: Move back to the 'sharedb' logger name in v1.0
var logger = require('loglevel').getLogger('sharedb:connection');

function connectionState(socket) {
if (socket.readyState === 0 || socket.readyState === 1) return 'connecting';
Expand Down Expand Up @@ -56,7 +58,9 @@ function Connection(socket) {
// state on the agent and read it in middleware
this.agent = null;

// TODO: Remove this as a breaking changes in v1.0, and use the 'sharedb' logger and its level
this.debug = false;
logger.setLevel(logger.levels.DEBUG);

this.state = connectionState(socket);

Expand Down Expand Up @@ -116,11 +120,11 @@ Connection.prototype.bindToSocket = function(socket) {
var data = (typeof event.data === 'string') ?
JSON.parse(event.data) : event.data;
} catch (err) {
console.warn('Failed to parse message', event);
logger.warn('Failed to parse message', event);
return;
}

if (connection.debug) console.log('RECV', JSON.stringify(data));
if (connection.debug) logger.debug('RECV', JSON.stringify(data));

var request = {data: data};
connection.emit('receive', request);
Expand Down Expand Up @@ -252,7 +256,7 @@ Connection.prototype.handleMessage = function(message) {
return;

default:
console.warn('Ignoring unrecognized message', message);
logger.warn('Ignoring unrecognized message', message);
}
};

Expand All @@ -274,7 +278,7 @@ Connection.prototype._handleBulkMessage = function(message, method) {
if (doc) doc[method](message.error);
}
} else {
console.error('Invalid bulk message', message);
logger.error('Invalid bulk message', message);
}
};

Expand Down Expand Up @@ -426,7 +430,7 @@ Connection.prototype.sendOp = function(doc, op) {
* Sends a message down the socket
*/
Connection.prototype.send = function(message) {
if (this.debug) console.log('SEND', JSON.stringify(message));
if (this.debug) logger.debug('SEND', JSON.stringify(message));

this.emit('send', message);
this.socket.send(JSON.stringify(message));
Expand Down
3 changes: 2 additions & 1 deletion lib/client/doc.js
@@ -1,6 +1,7 @@
var emitter = require('../emitter');
var ShareDBError = require('../error');
var types = require('../types');
var logger = require('loglevel').getLogger('sharedb');

/**
* A Doc is a client's view on a sharejs document.
Expand Down Expand Up @@ -832,7 +833,7 @@ Doc.prototype._opAcknowledged = function(message) {
} else if (message.v !== this.version) {
// We should already be at the same version, because the server should
// have sent all the ops that have happened before acknowledging our op
console.warn('Invalid version from server. Expected: ' + this.version + ' Received: ' + message.v, message);
logger.warn('Invalid version from server. Expected: ' + this.version + ' Received: ' + message.v, message);

// Fetching should get us back to a working document state
return this.fetch();
Expand Down
1 change: 1 addition & 0 deletions lib/client/index.js
Expand Up @@ -3,3 +3,4 @@ exports.Doc = require('./doc');
exports.Error = require('../error');
exports.Query = require('./query');
exports.types = require('../types');
exports.setLogLevel = require('loglevel').getLogger('sharedb').setLevel;
3 changes: 2 additions & 1 deletion lib/stream-socket.js
@@ -1,6 +1,7 @@
var Duplex = require('stream').Duplex;
var inherits = require('util').inherits;
var util = require('./util');
var logger = require('loglevel').getLogger('sharedb');

function StreamSocket() {
this.readyState = 0;
Expand Down Expand Up @@ -36,7 +37,7 @@ function ServerStream(socket) {
this.socket = socket;

this.on('error', function(error) {
console.warn('ShareDB client message stream error', error);
logger.warn('ShareDB client message stream error', error);
socket.close('stopped');
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -8,6 +8,7 @@
"async": "^1.4.2",
"deep-is": "^0.1.3",
"hat": "0.0.3",
"loglevel": "^1.6.1",
"make-error": "^1.1.1",
"ot-json0": "^1.0.1"
},
Expand Down

0 comments on commit 4f2f2dc

Please sign in to comment.