0.0.11 • Published 9 months ago

safetrack-logger v0.0.11

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
9 months ago

safetrack-logger NPM version NPM monthly downloads NPM total downloads

Stores the logs of the requests and responses to the APIs

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:

$ npm i safetrack-logger

Install with yarn:

$ 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)
#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

  1. Add env, mofile file {root-project}/deploy.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
  1. Modify file {root-project}/src/database/connection.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();
  },
...
  1. If add TYPEORM_SSL_LOG modify Dockerfile.dev and Dockerfile.prod. Add logger_ca.crt root project
...
# Add next COPY ormconfig* ./
COPY logger_ca* ./
...

Middleware

  1. Use middleware loggerMiddlware, modify file {root-project}/src/app.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.

0.0.10

10 months ago

0.0.11

9 months ago

0.0.9

10 months ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago