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

feat!: added option to send logs to elastic #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
29 changes: 0 additions & 29 deletions .github/workflows/lint.yml

This file was deleted.

50 changes: 50 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: pull_request

on: [pull_request]

jobs:
eslint:
name: Run TS Project eslint
runs-on: ubuntu-latest

steps:
- name: Check out TS Project Git repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install TS Project dependencies
run: npm install

- name: Run TS Project linters
uses: wearerequired/lint-action@v1
with:
github_token: ${{ secrets.github_token }}
# Enable linters
eslint: true
prettier: true
eslint_extensions: ts

test:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Check out TS Project Git repository
uses: actions/checkout@v3
- name: Runs Elasticsearch
uses: elastic/elastic-github-actions/elasticsearch@562b8b6ae4677da97273ff6bc4d630ce96ecbaa5
with:
stack-version: 8.3.3
security-enabled: false
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install TS Project dependencies
run: npm ci
- name: Run tests
run: npm run test
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# js logger

Javascript and typescript logger for MapColonies based on pino.
The logger supports three possible destinations for the logs:
- console
- file
- elasticsearch

## Usage
## Installation
```sh
npm install @map-colonies/js-logger
```

## Usage
### Basic Usage
```typescript
import jsLogger from '@map-colonies/js-logger';

Expand All @@ -14,14 +23,72 @@ logger.info('hello world');
logger.error({hello: 'world'});
```

### Pretty Print
```typescript
import jsLogger from '@map-colonies/js-logger';

const logger = jsLogger({}, { console: { enabled: true, prettyPrint: true } });

logger.info('test');
```

### File destination
```typescript
import jsLogger from '@map-colonies/js-logger';

const logger = jsLogger({}, { console: { enabled: false }, file: { enabled: true, path: 'logs/app.log' } });

logger.info('test');
```

### Elasticsearch destination
```typescript
const logger = jsLogger({}, { console: { enabled: false }, elastic: { enabled: true, node: 'http://localhost:9200' } });

logger.info('test');
```



for more detailed usage check the [pino documentation](https://github.com/pinojs/pino).

## Configuration
### Base
| name |type| default value | description
|---|---|---|---|
enabled | boolean | true| enables logging
level | string | 'info' | one of the supported level or silent to disable logging
prettyPrint | boolean |false| pretty print for developing purposes
redact | array | undefined| array of paths in object to be redacted from the log
destination | number / string | 1 | The stream to send the log to, or file
base | object | {pid: process.pid, hostname: os.hostname} | Key-value object added as child logger to each log line

### Destinations
| name |type| default value | description | env
|---|---|---|---|---|
console.enabled | boolean | true | Whether to enable the console destination | LOGGER_CONSOLE_ENABLED
console.prettyPrint | string | false | Pretty print the logs the console | LOGGER_CONSOLE_PRETTY_PRINT
console.destination | 'stdout' or 'stderr' | 'stdout' | The destination to use for the console logs | LOGGER_CONSOLE_DESTINATION
elastic.enabled | boolean | false | Whether to enable the elastic destination | LOGGER_ELASTIC_ENABLED
elastic.index | string | | The index to use for the logs | LOGGER_ELASTIC_INDEX
elastic.node | string | http://localhost:9200 | The node to connect to | LOGGER_ELASTIC_NODE
elastic.auth.username | string | | The username to use for authentication | LOGGER_ELASTIC_AUTH_USERNAME
elastic.auth.password | string | | The password to use for authentication | LOGGER_ELASTIC_AUTH_PASSWORD
elastic.auth.apiKey | string | | The api key to use for authentication | LOGGER_ELASTIC_AUTH_API_KEY
elastic.esVersion | number | 7 | The elasticsearch version to use | LOGGER_ELASTIC_ES_VERSION
elastic.flushInterval | number | 30000 | The interval to use for flushing the logs to the cluster | LOGGER_ELASTIC_FLUSH_INTERVAL
elastic.rejectUnauthorized | boolean | true | Whether to reject unauthorized connections | LOGGER_ELASTIC_REJECT_UNAUTHORIZED
elastic.type | string | | The type to use for the logs | LOGGER_ELASTIC_TYPE
elastic.debug | string | false | Whether to enable debug mode | LOGGER_ELASTIC_DEBUG
file.enabled | boolean | false | Whether to enable the file destination | - |
file.path | string | | The file path to use for the logs | - |

## Running tests
Make sure you have a running elastic cluster. You can run a local cluster using the following docker command:
```sh
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms750m -Xmx750m" --env "xpack.security.enabled=false" elasticsearch:8.11.1
```

To run the test use the following command:
```
npm run test
```
Loading
Loading