Skip to content

Commit a96d4b8

Browse files
authored
Logging client (#1)
Split from https://github.com/Countingup/browser-logging. It doesn't make sense to have a single package for the client & server when the client will be used as part of a web app and the server is node only.
1 parent 6e7575a commit a96d4b8

File tree

10 files changed

+1455
-3
lines changed

10 files changed

+1455
-3
lines changed

.github/workflows/action.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "fmt-check"
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v1
13+
- uses: actions/setup-node@v1
14+
with:
15+
node-version: 12
16+
- run: yarn install --frozen-lockfile
17+
- run: yarn run fmt-check

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
.idea/
12
node_modules
2-
3+
dist/
4+
yarn-error.log

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

.prettierrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"printWidth": 120,
3+
"trailingComma": "es5",
4+
"singleQuote": false
5+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Counting Limited
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# browser-logging-client
2+
3+
Sends browser logs to [browser-logging-server](https://www.npmjs.com/package/browser-logging-server).
4+
5+
This is useful for retrieving Cypress UI test browser logs.
6+
7+
Any message sent to `console.log`, `console.warn` and `console.error` will be sent to and logged by the logging server.
8+
9+
### Usage
10+
11+
Add the dependency to your project with:
12+
13+
```
14+
yarn add browser-logging-client
15+
```
16+
17+
or
18+
19+
```
20+
npm install browser-logging-client
21+
```
22+
23+
Then import & initialise at the root of your application.
24+
25+
```js
26+
import browserLoggingClient from "browser-logging-client";
27+
browserLoggingClient.initialise();
28+
```
29+
30+
Note calling `initialise` more than once has no effect.
31+
32+
Ensure the [server](https://www.npmjs.com/package/browser-logging-server) is running before starting your application.
33+
34+
## Publishing
35+
36+
Install `np` - https://github.com/sindresorhus/np:
37+
38+
```
39+
yarn global add np
40+
```
41+
42+
Run `np` and follow instructions.
43+
44+
## License
45+
46+
[MIT](https://opensource.org/licenses/MIT) License.

package.json

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,41 @@
22
"name": "browser-logging-client",
33
"version": "0.0.1",
44
"description": "Send logs from browser to node server",
5-
"main": "index.js",
65
"license": "MIT",
7-
"private": false
6+
"main": "./dist/index.js",
7+
"types": "./dist/index.d.ts",
8+
"repository": {
9+
"type": "git",
10+
"url": "[email protected]:Countingup/browser-logging-client.git"
11+
},
12+
"scripts": {
13+
"version": "npm run build",
14+
"postversion": "git push",
15+
"test": "echo No tests.",
16+
"build": "tsc",
17+
"fmt": "prettier --write \"./**/*.{md,js,ts,tsx,json}\"",
18+
"fmt-check": "prettier --list-different \"./**/*.{md,js,ts,tsx,json}\""
19+
},
20+
"files": [
21+
"src",
22+
"dist"
23+
],
24+
"devDependencies": {
25+
"husky": "^3.1.0",
26+
"lint-staged": "^9.4.3",
27+
"prettier": "^1.19.1",
28+
"typescript": "^3.7.2"
29+
},
30+
"dependencies": {},
31+
"lint-staged": {
32+
"*.{md,js,ts,tsx,json}": [
33+
"yarn run prettier --write",
34+
"git add"
35+
]
36+
},
37+
"husky": {
38+
"hooks": {
39+
"pre-commit": "lint-staged"
40+
}
41+
}
842
}

src/index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
let connected = false;
2+
let socket: WebSocket;
3+
let initialised = false;
4+
5+
const defaultLog = console.log;
6+
const defaultWarn = console.warn;
7+
const defaultError = console.error;
8+
9+
/**
10+
* Overrides console.log, warn & error to also send to logging server.
11+
* Allows retrieving logs when running Cypress tests.
12+
* Logging server must be running for this to work. See https://www.npmjs.com/package/browser-logging-server.
13+
*/
14+
export const initialise = (host: string = "localhost", port: number = 8888) => {
15+
if (initialised) return;
16+
initialised = true;
17+
18+
socket = new WebSocket(`ws://${host}:${port}`);
19+
socket.onopen = () => (connected = true);
20+
21+
console.log(`Piping browser logs to logging server at ws://${host}:${port}`);
22+
23+
console.log = pipeToLoggingServer(defaultLog, "INFO");
24+
console.warn = pipeToLoggingServer(defaultWarn, "WARN");
25+
console.error = pipeToLoggingServer(defaultError, "ERROR");
26+
};
27+
28+
const pipeToLoggingServer = (logger: (...args: any[]) => void, logLevel: string) => (...args: any[]) => {
29+
logger(...args);
30+
31+
sendToLoggingServer(Date.now(), logLevel, args, 0);
32+
};
33+
34+
const sendToLoggingServer = (timestamp: number, logLevel: string, message: any[], retryCount: number) => {
35+
if (!connected && retryCount < 10) {
36+
// websocket not connected - retry a bit later
37+
setTimeout(() => sendToLoggingServer(timestamp, logLevel, message, retryCount++), 200);
38+
return;
39+
}
40+
try {
41+
socket.send(JSON.stringify({ timestamp, logLevel, message }, replaceErrors));
42+
} catch (e) {
43+
defaultError("Error sending to logging server:", e);
44+
}
45+
};
46+
47+
// override error stringification (otherwise they display as "{}")
48+
const replaceErrors = (key: string, value: any) => {
49+
if (value instanceof Error) {
50+
const error: { [key: string]: any } = {};
51+
52+
Object.getOwnPropertyNames(value).forEach(key => {
53+
// @ts-ignore
54+
error[key] = value[key];
55+
});
56+
57+
return error;
58+
}
59+
60+
return value;
61+
};

tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"outDir": "./dist",
7+
"strict": true
8+
}
9+
}

0 commit comments

Comments
 (0)