Skip to content

Commit

Permalink
add README
Browse files Browse the repository at this point in the history
  • Loading branch information
hivivo committed Dec 30, 2020
1 parent 551a99a commit 0fa24a1
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 129 deletions.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# winston-firestore

Store winston logs into Firestore

## Usage

### Set up a new Web App in Firebase (Optional)

It's a good practice to set up a new Web app in Firebase to produce logs. You can also use your existing app instead.

You'll need the following configurations later:

```javascript
var firebaseConfig = {
apiKey: "AIzaSyAThisIsASampleAPIkeyELHHZNZu8",
authDomain: "someapp.firebaseapp.com",
projectId: "someapp",
storageBucket: "someapp.appspot.com",
messagingSenderId: "508123456421",
appId: "1:508123456421:web:42141234c987abcdefgfcc",
measurementId: "G-WB0ABCD1B8"
};
```

### Install winston-firestore

Install the package by

```bash
npm install winston-firestore
```

or

```bash
yarn add winston-firestore
```

Add `FirestoreTransport` transport into winston:

```typescript
import { FirestoreTransport } from 'winston-firestore';

winston.createLogger({
...
transports: [
...,
new FirestoreTransport({
firebaseConfig: {
apiKey: "AIzaSyAThisIsASampleAPIkeyELHHZNZu8",
authDomain: "someapp.firebaseapp.com",
projectId: "someapp",
storageBucket: "someapp.appspot.com",
messagingSenderId: "508123456421",
appId: "1:508123456421:web:42141234c987abcdefgfcc",
measurementId: "G-WB0ABCD1B8"
},
parentCollectionName: '<collection>',
parentDocumentId: '<doucment>',
collectionName: '<name of the logs collection>',
}),
...,
],
...
})
```

Use `firebaseConfig` from the previous step.

Now we need to configure three necessary variables to make it work.

Our transport will push any log into a sub collection of a specified firestore document.

For example, if you want to save all logs into `logs` sub collection of `/Computers/mycomputer` document, you will need to configure like this:

```typescript
parentCollectionName: 'Computers',
parentDocumentId: 'mycomputer',
collectionName: 'logs',
```

and all logs will be pushed into `/Computers/mycomputer/logs` sub collection with auto generated ids.

### Setup proper rules for Firestore

You'll need to setup proper rules for Firestore to receive those logs. An easy but dangerous way could be:

```
match /Computers/{computer} {
match /logs/{log} {
allow write: if true;
}
allow read, write: if false;
}
```

Please consider security risks thoughtfully.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"dependencies": {
"firebase": "^8.2.1",
"rimraf": "^3.0.2",
"winston": "^3.3.3",
"winston-transport": "^4.4.0"
},
"devDependencies": {
Expand All @@ -29,6 +28,7 @@
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"jest": "^26.6.3",
"logform": "^2.2.0",
"prettier": "^2.2.1",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
Expand Down
23 changes: 15 additions & 8 deletions src/firestore-transport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Transport from 'winston-transport';
import { LoggerOptions } from 'winston';
import TransportStream, { TransportStreamOptions } from 'winston-transport';
import firebase from 'firebase/app';
import 'firebase/firestore';

export interface FirestoreTransportOptions extends LoggerOptions {
export interface FirestoreTransportOptions extends TransportStreamOptions {
firebaseConfig: {
apiKey: string;
authDomain: string;
Expand All @@ -14,11 +13,12 @@ export interface FirestoreTransportOptions extends LoggerOptions {
databaseURL?: string;
measurementId: string;
};
parentCollectionName: string;
parentDocumentId: string;
collectionName: string;
documentId: string;
}

export class FirestoreTransport extends Transport {
export class FirestoreTransport extends TransportStream {
protected options: FirestoreTransportOptions;
protected store: firebase.firestore.Firestore;

Expand All @@ -32,9 +32,16 @@ export class FirestoreTransport extends Transport {
}

async log(info: any, callback: () => void) {
console.log(info);

this.emit('logged', info);
try {
await this.store
.collection(this.options.parentCollectionName)
.doc(this.options.parentDocumentId)
.collection(this.options.collectionName)
.add(info);
this.emit('logged', info);
} catch (error) {
this.emit('warn', error);
}

if (callback) {
setImmediate(callback);
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
"incremental": true,
"declarationMap": true,
"esModuleInterop": true,
},
"exclude": ["node_modules"]
"exclude": ["node_modules", "dist"]
}
Loading

0 comments on commit 0fa24a1

Please sign in to comment.