Skip to content

Commit

Permalink
Merge pull request #1289 from FoalTS/logger-format
Browse files Browse the repository at this point in the history
Remove setting `loggerFormat` and add `logger.logHttpRequests`.
  • Loading branch information
LoicPoullain authored Aug 22, 2024
2 parents c2c4c49 + 7218835 commit 7b994a9
Show file tree
Hide file tree
Showing 28 changed files with 88 additions and 85 deletions.
1 change: 1 addition & 0 deletions docs/blog/version-5.0-release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Version 5.0 of [Foal](https://foalts.org/) is out!
## Logging
- The `Logger.addLogContext(key, value)` method now accepts a record as parameter: `Logger.addLogContext(context)`. This makes the function's signature more consistent with other logging methods (`info`, `warn`, etc.) and allows multiple keys/values to be passed at once.
- The deprecated `settings.loggerFormat` configuration has been removed. If you want to disable HTTP logging, set `settings.logger.logHttpRequests` to false instead.
## Removal of deprecated components
Expand Down
33 changes: 7 additions & 26 deletions docs/docs/common/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ Foal provides an advanced built-in logger. This page shows how to use it.

## Recommended Configuration

*config/default.json*
```json
{
"settings": {
"loggerFormat": "foal"
}
}
```

*config/development.json*
```json
{
Expand Down Expand Up @@ -143,7 +134,7 @@ If you wish to completly mask logs, you can use the `none` format.

Each request received by Foal is logged with the INFO level.

With the configuration key `settings.loggerFormat` set to `"foal"`, the messages start with `HTTP request -` and end with the request method and URL. The log parameters include the response status code and content length as well as the response time and the request method and URL.
The messages start with `HTTP request -` and end with the request method and URL. The log parameters include the response status code and content length as well as the response time and the request method and URL.

> Note: the query parameters are not logged to avoid logging sensitive data (such as an API key).
Expand All @@ -162,26 +153,16 @@ const app = await createApp({
})
```

### Formatting the log message (deprecated)

If you wish to customize the HTTP log messages, you can set the value of the `loggerFormat.loggerFormat` configuration to a format supported by [morgan](https://www.npmjs.com/package/morgan). With this technique, no parameters will be logged though.

```json
{
"settings": {
"loggerFormat": "tiny"
}
}
```

### Disabling HTTP Request Logging

In some scenarios and environments, you might want to disable HTTP request logging. You can achieve this by setting the `loggerFormat` configuration option to `none`.
In some scenarios and environments, you might want to disable HTTP request logging. You can achieve this by setting the `logger.logHttpRequests` configuration option to `false`.

```json
{
"settings": {
"loggerFormat": "none"
"logger": {
"logHttpRequests": false
}
}
}
```
Expand Down Expand Up @@ -216,12 +197,12 @@ When an error is thrown (or rejected) in a hook, controller or service and is no

### Disabling Error Logging

In some scenarios, you might want to disable error logging. You can achieve this by setting the `allErrors` configuration option to false.
In some scenarios, you might want to disable error logging. You can achieve this by setting the `logErrors` configuration option to false.

```json
{
"settings": {
"allErrors": false
"logErrors": false
}
}
```
Expand Down
3 changes: 2 additions & 1 deletion packages/acceptance-tests/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
settings:
loggerFormat: none
logger:
logHttpRequests: false
1 change: 0 additions & 1 deletion packages/cli/src/generate/specs/app/config/default.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"port": "env(PORT)",
"settings": {
"loggerFormat": "foal",
"session": {
"store": "@foal/typeorm"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"port": "env(PORT)",
"settings": {
"loggerFormat": "foal"
},
"database": {
"type": "mongodb",
"url": "mongodb://localhost:27017/test-foo-bar"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
port: env(PORT)

settings:
loggerFormat: foal

database:
type: mongodb
url: mongodb://localhost:27017/test-foo-bar
1 change: 0 additions & 1 deletion packages/cli/src/generate/specs/app/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
port: env(PORT)

settings:
loggerFormat: foal
session:
store: '@foal/typeorm'

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/generate/specs/app/config/e2e.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"settings": {
"loggerFormat": "none"
"logger": {
"logHttpRequests": false
}
},
"database": {
"database": "./e2e_db.sqlite3",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/generate/specs/app/config/e2e.mongodb.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"settings": {
"loggerFormat": "none"
"logger": {
"logHttpRequests": false
}
},
"database": {
"url": "mongodb://localhost:27017/e2e-test-foo-bar"
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/generate/specs/app/config/e2e.mongodb.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
loggerFormat: 'none'
logger:
logHttpRequests: false

database:
url: mongodb://localhost:27017/e2e-test-foo-bar
3 changes: 2 additions & 1 deletion packages/cli/src/generate/specs/app/config/e2e.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
loggerFormat: 'none'
logger:
logHttpRequests: false

database:
database: './e2e_db.sqlite3'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"port": "env(PORT)",
"settings": {
"loggerFormat": "foal",
"session": {
"store": "@foal/typeorm"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"port": "env(PORT)",
"settings": {
"loggerFormat": "foal"
},
"database": {
"type": "mongodb",
"url": "mongodb://localhost:27017//* kebabName */"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
port: env(PORT)

settings:
loggerFormat: foal

database:
type: mongodb
url: mongodb://localhost:27017//* kebabName */
1 change: 0 additions & 1 deletion packages/cli/src/generate/templates/app/config/default.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
port: env(PORT)

settings:
loggerFormat: foal
session:
store: '@foal/typeorm'

Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/generate/templates/app/config/e2e.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"settings": {
"loggerFormat": "none"
"logger": {
"logHttpRequests": false
}
},
"database": {
"database": "./e2e_db.sqlite3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"settings": {
"loggerFormat": "none"
"logger": {
"logHttpRequests": false
}
},
"database": {
"url": "mongodb://localhost:27017/e2e-/* kebabName */"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
loggerFormat: 'none'
logger:
logHttpRequests: false

database:
url: mongodb://localhost:27017/e2e-/* kebabName */
3 changes: 2 additions & 1 deletion packages/cli/src/generate/templates/app/config/e2e.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
settings:
loggerFormat: 'none'
logger:
logHttpRequests: false

database:
database: './e2e_db.sqlite3'
Expand Down
51 changes: 40 additions & 11 deletions packages/core/src/express/create-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('createApp', () => {
Config.remove('settings.debug');
Config.remove('settings.bodyParser.limit');
Config.remove('settings.cookieParser.secret');
Config.remove('settings.loggerFormat');
Config.remove('settings.logger.logHttpRequests');
Config.remove('settings.logger.format');
Config.remove('settings.staticFiles.cacheControl');
});
Expand Down Expand Up @@ -766,9 +766,37 @@ describe('createApp', () => {
}
});

context('given the configuration "settings.loggerFormat" is set to "foal"', () => {
context('given the configuration "settings.logger.logHttpRequests" is set to false', () => {
it('should NOT log the request.', async () => {
Config.set('settings.logger.logHttpRequests', false);

class AppController {
@Get('/a')
getA(ctx: Context) {
return new HttpResponseOK('a');
}
}

const serviceManager = new ServiceManager();

const logger = serviceManager.get(Logger);
const loggerMock = mock.method(logger, 'info', () => {}).mock;

const app = await createApp(AppController, {
serviceManager
});

await request(app)
.get('/a')
.expect(200);

strictEqual(loggerMock.callCount(), 0);
});
});

context('given the configuration "settings.logger.logHttpRequests" is set to true', () => {
it('should log the request with a detailed message and detail parameters.', async () => {
Config.set('settings.loggerFormat', 'foal');
Config.set('settings.logger.logHttpRequests', true);

class AppController {
@Get('/a')
Expand Down Expand Up @@ -809,7 +837,7 @@ describe('createApp', () => {
});

it('should use the options.getHttpLogParams if provided', async () => {
Config.set('settings.loggerFormat', 'foal');
Config.set('settings.logger.logHttpRequests', true);

class AppController {
@Get('/a')
Expand Down Expand Up @@ -852,10 +880,8 @@ describe('createApp', () => {
});
});

context('given the configuration "settings.loggerFormat" is set to a value different from "none" or "foal"', () => {
it('should log a warning message.', async () => {
Config.set('settings.loggerFormat', 'dev');

context('given the configuration "settings.logger.logHttpRequests" is not set', () => {
it('should behave like the configuration is set to true.', async () => {
class AppController {
@Get('/a')
getA(ctx: Context) {
Expand All @@ -866,14 +892,17 @@ describe('createApp', () => {
const serviceManager = new ServiceManager();

const logger = serviceManager.get(Logger);
const loggerMock = mock.method(logger, 'warn', () => {}).mock;
const loggerMock = mock.method(logger, 'info', () => {}).mock;

await createApp(AppController, {
const app = await createApp(AppController, {
serviceManager
});

await request(app)
.get('/a')
.expect(200);

strictEqual(loggerMock.callCount(), 1);
strictEqual(loggerMock.calls[0].arguments[0], '[CONFIG] Using another format than "foal" for "settings.loggerFormat" is deprecated.');
});
});

Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/express/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,8 @@ export async function createApp(
});

// Log requests.
const loggerFormat = Config.get(
'settings.loggerFormat',
'string',
'[:date] ":method :url HTTP/:http-version" :status - :response-time ms'
);
if (loggerFormat === 'foal') {
const shouldLogHttpRequests = Config.get('settings.logger.logHttpRequests', 'boolean', true);
if (shouldLogHttpRequests) {
const getHttpLogParams = options.getHttpLogParams || getHttpLogParamsDefault;
app.use(morgan(
(tokens: any, req: any, res: any) => JSON.stringify(getHttpLogParams(tokens, req, res)),
Expand All @@ -140,9 +136,6 @@ export async function createApp(
},
}
))
} else if (loggerFormat !== 'none') {
logger.warn('[CONFIG] Using another format than "foal" for "settings.loggerFormat" is deprecated.');
app.use(morgan(loggerFormat));
}

app.use(protectionHeaders);
Expand Down
1 change: 0 additions & 1 deletion packages/examples/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ port: 3001
settings:
openapi:
useHooks: true
loggerFormat: foal
session:
secret: 'my secret'
staticPath: 'public/'
Expand Down
Loading

0 comments on commit 7b994a9

Please sign in to comment.