# safetrack-logger

> Stores the logs of the requests and responses to the APIs

Latest version **0.0.12** (published 2026-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install safetrack-logger
pnpm add safetrack-logger
yarn add safetrack-logger
bun add safetrack-logger
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2026-02-27 |
| First published | 2022-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 28.9 KB |
| Known vulnerabilities | 0 (+7 in 2 direct dependencies) |
| Install scripts | no |
| Author | Edwin Bastidas |
| Maintainers | safetrack |
| Keywords | Safetrack, Log, TypeORM |

## Links

- npm: https://www.npmjs.com/package/safetrack-logger
- Repository: https://bitbucket.org/safetrackcloud/safetrack-logger
- Homepage: https://bitbucket.org/safetrackcloud/safetrack-logger#readme
- npm.io page: https://npm.io/package/safetrack-logger

## Dependencies (6)

- [typedi](https://npm.io/package/typedi.md) ^0.10.0
- [mongodb](https://npm.io/package/mongodb.md) ^4.7.0
- [typeorm](https://npm.io/package/typeorm.md) ^0.2.41
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.5.1
- [routing-controllers](https://npm.io/package/routing-controllers.md) ^0.9.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 0.0.12 (latest) — 2026-02-27
- 0.0.11 — 2023-08-07
- 0.0.10 — 2023-07-06
- 0.0.9 — 2023-07-04
- 0.0.8 — 2022-07-08
- 0.0.7 — 2022-07-06
- 0.0.6 — 2022-07-05
- 0.0.5 — 2022-06-30
- 0.0.4 — 2022-06-30
- 0.0.3 — 2022-06-30
- 0.0.2 — 2022-06-30
- 0.0.1 — 2022-06-29

## README

# safetrack-logger [![NPM version](https://img.shields.io/npm/v/safetrack-logger.svg?style=flat)](https://www.npmjs.com/package/safetrack-logger) [![NPM monthly downloads](https://img.shields.io/npm/dm/safetrack-logger.svg?style=flat)](https://npmjs.org/package/safetrack-logger) [![NPM total downloads](https://img.shields.io/npm/dt/safetrack-logger.svg?style=flat)](https://npmjs.org/package/safetrack-logger)

Stores the logs of the requests and responses to the APIs

```sh
requestTime: request time
url: endpoint service
username: username request
roles: user roles
responseStatus: response status (200 | 400 | 500 etc)
verb: method (POST | GET | PUT | DETELE)
requestHeader: request header
requestBody: request body 
responseBody: response body
requestIp: request ip
requestSsoo: user agent
```

## Install

Install with [npm](https://www.npmjs.com/):

```sh
$ npm i safetrack-logger
```

Install with [yarn](https://yarnpkg.com/):

```sh
$ yarn add safetrack-logger
```

## Usage

**Project name**

1. Check for package.json, if it exists `name` is returned

**Configura conexión**

1. Define environment variables (also insert into .env.example)

```sh
#TYPEORM LOG
TYPEORM_TYPE_LOG = mongodb
TYPEORM_HOST_LOG = localhost
TYPEORM_USERNAME_LOG = safetrack
TYPEORM_PASSWORD_LOG = 
TYPEORM_DATABASE_LOG = safetrack
TYPEORM_PORT_LOG = 27017
TYPEORM_SYNCHRONIZE_LOG = false
TYPEORM_LOGGING_LOG = true
TYPEORM_SSL_LOG = logger_ca.crt
```
`TYPEORM_SSL_LOG` is optional, connect with ssl. If you add follow step `4`

2. Add env, mofile file `{root-project}/deploy.yaml`

```yaml
    ...
    spec:
      containers:
      - name: safetrack-business-instance-qs
        image: {{image}}
        env:
        ...
        // add next lines
        - name: TYPEORM_TYPE_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_type_log
        - name: TYPEORM_HOST_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_host_log
        - name: TYPEORM_USERNAME_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_username_log
        - name: TYPEORM_PASSWORD_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_password_log
        - name: TYPEORM_DATABASE_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_database_log
        - name: TYPEORM_PORT_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_port_log
        - name: TYPEORM_SYNCHRONIZE_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_synchronize_log
        - name: TYPEORM_LOGGING_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_logging_log
        - name: TYPEORM_SSL_LOG
          valueFrom:
            secretKeyRef:
              name: safetrack-backend-secret
              key: typeorm_ssl_log
```

3. Modify file `{root-project}/src/database/connection.ts`

```ts
import { getConnection, Connection, createConnections } from 'typeorm';
import config from '@ormconfig';
import { getConnectionLog } from 'safetrack-logger';

const connection = {
  async create(callback?: (c: Connection[]) => void): Promise<void> {
    try {
      const connection = await createConnections([config]);
      connection.push(await getConnectionLog());
      if (callback) {
        callback(connection);
      }
    } catch (error) {
      throw new Error(`ERROR: Creating test db connection: ${error}`);
    }
  },

  async close(): Promise<void> {
    await getConnection().close();
    await getConnection('log').close();
  },
...
```

4. If add `TYPEORM_SSL_LOG` modify `Dockerfile.dev` and `Dockerfile.prod`. Add `logger_ca.crt` root project

```yml
...
# Add next COPY ormconfig* ./
COPY logger_ca* ./
...
```


**Middleware**

1. Use middleware `loggerMiddlware`, modify file `{root-project}/src/app.ts`

```ts
...
import { loggerMiddleware } from 'safetrack-logger';

// Add befere useExpressServer(app, routingControllersOptions);
app.use(routingControllersOptions.routePrefix, loggerMiddleware([<arrayPaths>]));

// Wrap server with routing-controllers
useExpressServer(app, routingControllersOptions);
```
`arrayPaths` is optional, are the paths with sensitive data that you do not want to show in the response and in the request.
Example `['instance/:param','business-instance/instance/:param]`, in this case it will ignore the paths `instance/{id}` and `business-instance/instance/{instanceId}`.  Use `:param` where param dynamic.

---
_Source: https://npm.io/package/safetrack-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
