Skip to content

Commit

Permalink
Replace loggerFormat with logger.logHttpRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicPoullain committed Aug 22, 2024
1 parent 139e902 commit 3e6fb8d
Show file tree
Hide file tree
Showing 27 changed files with 85 additions and 83 deletions.
29 changes: 5 additions & 24 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
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
9 changes: 6 additions & 3 deletions packages/social/src/abstract-provider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('AbstractProvider', () => {
Config.set('settings.social.example.clientId', clientId);
Config.set('settings.social.example.clientSecret', clientSecret);
Config.set('settings.social.example.redirectUri', redirectUri);
Config.set('settings.loggerFormat', 'none');
Config.set('settings.logger.logHttpRequests', false);

provider = createService(ConcreteProvider);
});
Expand All @@ -175,6 +175,7 @@ describe('AbstractProvider', () => {
Config.remove('settings.social.example.clientId');
Config.remove('settings.social.example.clientSecret');
Config.remove('settings.social.example.redirectUri');
Config.remove('settings.logger.logHttpRequests');
Config.remove('settings.social.cookie.secure');
Config.remove('settings.social.cookie.domain');
});
Expand Down Expand Up @@ -734,7 +735,7 @@ describe('Abstract Provider With PKCE', () => {
Config.set('settings.social.example.clientId', clientId);
Config.set('settings.social.example.clientSecret', clientSecret);
Config.set('settings.social.example.redirectUri', redirectUri);
Config.set('settings.loggerFormat', 'none');
Config.set('settings.logger.logHttpRequests', false);

provider = createService(ConcreteProvider);
});
Expand All @@ -743,6 +744,7 @@ describe('Abstract Provider With PKCE', () => {
Config.remove('settings.social.example.clientId');
Config.remove('settings.social.example.clientSecret');
Config.remove('settings.social.example.redirectUri');
Config.remove('settings.logger.logHttpRequests');
Config.remove('settings.social.cookie.secure');
Config.remove('settings.social.cookie.domain');
});
Expand Down Expand Up @@ -934,7 +936,7 @@ describe('Abstract Provider With PKCE and Plain Method', () => {
Config.set('settings.social.example.clientId', clientId);
Config.set('settings.social.example.clientSecret', clientSecret);
Config.set('settings.social.example.redirectUri', redirectUri);
Config.set('settings.loggerFormat', 'none');
Config.set('settings.logger.logHttpRequests', false);

provider = createService(ConcreteProvider);
});
Expand All @@ -943,6 +945,7 @@ describe('Abstract Provider With PKCE and Plain Method', () => {
Config.remove('settings.social.example.clientId');
Config.remove('settings.social.example.clientSecret');
Config.remove('settings.social.example.redirectUri');
Config.remove('settings.logger.logHttpRequests');
Config.remove('settings.social.cookie.secure');
Config.remove('settings.social.cookie.domain');
});
Expand Down
4 changes: 2 additions & 2 deletions packages/social/src/facebook-provider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('FacebookProvider', () => {

beforeEach(() => {
provider = createService(FacebookProvider2);
Config.set('settings.loggerFormat', 'none');
Config.set('settings.logger.logHttpRequests', false);
});

afterEach(() => {
Config.remove('settings.loggerFormat');
Config.remove('settings.logger.logHttpRequests');
if (server) {
server.close();
}
Expand Down
Loading

0 comments on commit 3e6fb8d

Please sign in to comment.