djfdyuruiry / ts-lambda-api

Build REST API's using Typescript & AWS Lambda
MIT License
54 stars 15 forks source link

Logging Settings Have No Effect #20

Closed ktersius closed 5 years ago

ktersius commented 5 years ago

Setting logging to false or to an {} with settings does not seem to apply them.

import * as path from 'path';

import { AppConfig, ApiLambdaApp } from 'ts-lambda-api';
import { APIGatewayEvent } from 'aws-lambda';

const appConfig = new AppConfig();

appConfig.base = '/base';
appConfig.version = 'v1';
appConfig.logger = false; // This does not work.

const controllersPath = path.join(__dirname, 'controllers');
const app = new ApiLambdaApp(controllersPath, appConfig);

export async function handler(event: APIGatewayEvent, context) {
  event.multiValueHeaders = event.multiValueHeaders || {}; //Prevent issue https://github.com/jeremydaly/lambda-api/issues/73
  return await app.run(event, context);
}
djfdyuruiry commented 5 years ago

Hi,

Maybe I am not understanding the issue, but I have tried the two values you have suggested and both work as expected.

Given the below endpoint in a controller:

@GET("/")
public sayHello(@request request: Request): Message {
    request.log.info("Hello!")

    return {
        text: "hello"
    }
}
  1. Not setting logger at all (default behaviour):
2019-06-06T22:06:25.817Z INFO Server - Registering OpenAPI endpoints
2019-06-06T22:06:25.842Z WARN ApiConsoleApp - To simulate an AWS ALB or Application Gateway the max size for requests is limited to 1024kb
2019-06-06T22:06:25.843Z INFO ApiConsoleApp - OpenAPI enabled, configuring SwaggerUI to be available @ http://*:8080/swagger
2019-06-06T22:06:25.854Z INFO ApiConsoleApp - Listening for HTTP requests on http://*:8080 ...
2019-06-06T22:07:04.945Z INFO Server - Processing API request event for path: /
2019-06-06T22:07:04.948Z INFO Endpoint - [GET] / - Invoking endpoint
2019-06-06T22:07:04.953Z INFO Endpoint - [GET] / - Endpoint invoked successfully, returning response
{"level":"info","time":1559858824953,"id":null,"route":"/","method":"GET","msg":"Hello!","timer":7,"int":"apigateway"}
{"level":"access","time":1559858824955,"id":null,"route":"/","method":"GET","timer":9,"int":"apigateway","statusCode":200,"coldStart":true,"count":1,"path":"/","ua":"curl/7.58.0","device":"unknown","country":"unknown","version":"v1"}
  1. Setting logger to false
2019-06-06T22:10:21.403Z INFO Server - Registering OpenAPI endpoints
2019-06-06T22:10:21.413Z WARN ApiConsoleApp - To simulate an AWS ALB or Application Gateway the max size for requests is limited to 1024kb
2019-06-06T22:10:21.414Z INFO ApiConsoleApp - OpenAPI enabled, configuring SwaggerUI to be available @ http://*:8080/swagger
2019-06-06T22:10:21.422Z INFO ApiConsoleApp - Listening for HTTP requests on http://*:8080 ...
2019-06-06T22:10:55.304Z INFO Server - Processing API request event for path: /
2019-06-06T22:10:55.309Z INFO Endpoint - [GET] / - Invoking endpoint
2019-06-06T22:10:55.315Z INFO Endpoint - [GET] / - Endpoint invoked successfully, returning response
  1. Setting Logger to an empty object, {}
2019-06-06T22:11:59.949Z INFO Server - Registering OpenAPI endpoints
2019-06-06T22:11:59.961Z WARN ApiConsoleApp - To simulate an AWS ALB or Application Gateway the max size for requests is limited to 1024kb
2019-06-06T22:11:59.962Z INFO ApiConsoleApp - OpenAPI enabled, configuring SwaggerUI to be available @ http://*:8080/swagger
2019-06-06T22:11:59.970Z INFO ApiConsoleApp - Listening for HTTP requests on http://*:8080 ...
2019-06-06T22:12:08.865Z INFO Server - Processing API request event for path: /
2019-06-06T22:12:08.870Z INFO Endpoint - [GET] / - Invoking endpoint
2019-06-06T22:12:08.880Z INFO Endpoint - [GET] / - Endpoint invoked successfully, returning response

Tested using ts-lambda-api-local

ktersius commented 5 years ago

I think I was expecting no log output if you set logging to false. You can close this then.

djfdyuruiry commented 5 years ago

Ok, going to close now.

If you want to disable all logging, you also need to set the serverLogger field to change the log level to off:

import * as path from 'path';

import { AppConfig, ApiLambdaApp, LogLevel } from 'ts-lambda-api';
import { APIGatewayEvent } from 'aws-lambda';

const appConfig = new AppConfig();

appConfig.base = '/base';
appConfig.version = 'v1';
appConfig.logger = false; 
appConfig.serverLogger.level = LogLevel.off; 

const controllersPath = path.join(__dirname, 'controllers');
const app = new ApiLambdaApp(controllersPath, appConfig);

export async function handler(event: APIGatewayEvent, context) {
  event.multiValueHeaders = event.multiValueHeaders || {}; //Prevent issue https://github.com/jeremydaly/lambda-api/issues/73
  return await app.run(event, context);
}