Skip to content

Commit

Permalink
Merge pull request #449 from zazuko/log-format
Browse files Browse the repository at this point in the history
[core] Add `server.logFormat` configuration option
  • Loading branch information
ludovicm67 authored Jul 30, 2024
2 parents aa83b8d + 27d3bc1 commit 86c9727
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-avocados-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trifid-core": minor
---

Introduce a `server.logFormat` configuration option, to either output JSON or pretty-formated logs.
1 change: 1 addition & 0 deletions packages/core/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ server:
listener:
port: 8080
logLevel: debug
logFormat: pretty

globals:
value: config
Expand Down
23 changes: 14 additions & 9 deletions packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import handler from './lib/config/handler.js'
import {
defaultHost,
defaultLogLevel,
defaultLogFormat,
defaultPort,
} from './lib/config/default.js'
import pluginsAssembler from './lib/plugins/assembler.js'
Expand Down Expand Up @@ -57,6 +58,9 @@ const trifid = async (config, additionalPlugins = {}) => {

const serverOptions = fullConfig?.server?.options || {}

// Template configuration
const template = fullConfig?.template || {}

// Dynamic server configuration
const portFromConfig = fullConfig?.server?.listener?.port
const port = (portFromConfig === 0 || portFromConfig === '0') ? 0 : (portFromConfig || defaultPort)
Expand All @@ -65,18 +69,19 @@ const trifid = async (config, additionalPlugins = {}) => {

// Logger configuration
const logLevel = fullConfig?.server?.logLevel || defaultLogLevel

// Template configuration
const template = fullConfig?.template || {}

// Custom logger instance
const logger = pino({
const logFormat = fullConfig?.server?.logFormat || defaultLogFormat
const loggerConfig = {
name: 'trifid-core',
level: logLevel,
transport: {
}
if (logFormat === 'pretty') {
loggerConfig.transport = {
target: 'pino-pretty',
},
})
}
}

// Custom logger instance
const logger = pino(loggerConfig)

const server = fastify({
logger: false,
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export const maxDepth = 50
export const defaultPort = 8080
export const defaultHost = '0.0.0.0'
export const defaultLogLevel = 'info'
export const defaultLogFormat = 'pretty'
7 changes: 7 additions & 0 deletions packages/core/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
"silent"
]
},
"logFormat": {
"type": "string",
"enum": [
"json",
"pretty"
]
},
"options": {
"type": "object",
"additionalProperties": true
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @property {string} [server.listener.host] The host to listen on.
* @property {number|string} [server.listener.port] The port to listen on.
* @property {"fatal"|"error"|"warn"|"info"|"debug"|"trace"|"silent"} [server.logLevel] The log level.
* @property {"pretty"|"json"} [server.logFormat] The log format.
* @property {Object.<string, any>} [server.options] Server options.
* @property {Object.<string, any>} [globals] Global settings.
* @property {Object.<string, any>} [template] Template settings.
Expand Down
1 change: 1 addition & 0 deletions packages/entity-renderer/examples/config/trifid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

server:
logLevel: debug
logFormat: pretty

globals:
datasetBaseUrl: http://localhost:3000/
Expand Down

0 comments on commit 86c9727

Please sign in to comment.