Skip to content

Commit

Permalink
F #5422: Add option to truncate logs (#3039)
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Hansson <[email protected]>
Co-authored-by: Tino Vázquez <[email protected]>
  • Loading branch information
vichansson and tinova authored Apr 23, 2024
1 parent bdaf644 commit b9d08e6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/fireedge/etc/fireedge-server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ subscriber_endpoint: tcp://localhost:2101
#
debug_level: 2

# Maximum length of log messages (chars)
# Messages exceeding this limit will be truncated
# -1 => No limit
truncate_max_length: 150

################################################################################
# Global API Timeout
################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ const AutocompleteController = memo(

const selected = multiple
? renderValue ?? []
: values.find(({ value }) => value === renderValue) ?? null
: values.find(({ value }) => value === renderValue) || renderValue

const handleChange = useCallback(
(_, newValue) => {
const newValueToChange = multiple
? newValue?.map((value) =>
// In case that is an object, get value attribute
['string', 'number'].includes(typeof value) ? value : value.value
typeof value === 'object' ? value.value : value
)
: newValue?.value
: typeof newValue === 'object'
? newValue.value
: newValue

onChange(newValueToChange ?? '')
if (typeof onConditionChange === 'function') {
Expand All @@ -70,24 +71,31 @@ const AutocompleteController = memo(
color="secondary"
onBlur={onBlur}
onChange={handleChange}
onInputChange={(_, newInputValue, reason) => {
// Processes the input in freesolo mode
if (
reason === 'input' &&
(fieldProps?.freeSolo || !optionsOnly) &&
!multiple
) {
onChange(newInputValue)
if (typeof onConditionChange === 'function') {
onConditionChange(newInputValue)
}
}
}}
options={values}
value={selected}
multiple={multiple}
freeSolo={!optionsOnly}
renderTags={(tags, getTagProps) =>
// render when freesolo prop
tags.map((tag, index) => {
// When the component is multiple and has values, map to show the label that corresponds to the value
const labelTag =
values &&
values.length > 0 &&
values.find((item) => item.value === tag)
? values.find((item) => item.value === tag).text
: tag
values.find((item) => item.value === tag)?.text || tag

return (
<Chip
key={labelTag}
key={index}
size="small"
variant="outlined"
label={labelTag}
Expand All @@ -96,8 +104,12 @@ const AutocompleteController = memo(
)
})
}
getOptionLabel={(option) => option.text}
isOptionEqualToValue={(option) => option.value === renderValue}
getOptionLabel={(option) =>
typeof option === 'object' ? option.text : option
}
isOptionEqualToValue={(option, value) =>
typeof option === 'object' ? option.value === value : option === value
}
renderInput={({ inputProps, ...inputParams }) => (
<TextField
label={<Translate word={label} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ const NAME = {
name: 'name',
label: T.Name,
type: INPUT_TYPES.AUTOCOMPLETE,
disableEnter: true,
optionsOnly: false,
multiple: false,
values: () => useSelector((state) => state.persistent.userInputSuggestionsVR),
validation: string()
.trim()
.required()
.default(() => undefined),
grid: { sm: 6, md: 4 },
fieldProps: { freeSolo: true },
}

/** @type {Field} Type field */
Expand Down
2 changes: 1 addition & 1 deletion src/fireedge/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const appConfig = getFireedgeConfig()
genFireedgeKey()

// set logger
initLogger(appConfig.debug_level)
initLogger(appConfig.debug_level, appConfig.truncate_max_length)

// destructure imports
const unsecureServer = http.createServer
Expand Down
1 change: 1 addition & 0 deletions src/fireedge/src/server/utils/constants/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const defaults = {
defaultProvisionPath: internalProvisionPath,
defaultProvidersConfigPath: 'providers.d',
defaultLogsLevels: ['error', 'warm', 'info', 'http', 'verbose', 'debug'],
defaultLogMessageLength: 100,
defaultTypeLog: 'prod',
defaultWebpackMode: 'development',
defaultProductionWebpackMode: 'production',
Expand Down
33 changes: 28 additions & 5 deletions src/fireedge/src/server/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,51 @@
const { env } = require('process')
const { global } = require('window-or-global')
const { transports, format, createLogger } = require('winston')
const { printf } = format
const { sprintf } = require('sprintf-js')
const morgan = require('morgan')
const _ = require('lodash')
const { defaults } = require('server/utils/constants')
const { defaultWebpackMode, defaultLogsLevels } = defaults
const { defaultWebpackMode, defaultLogsLevels, defaultLogMessageLength } =
defaults

let logger = null

const getTruncFormat = (truncateMaxLength) =>
printf(({ timestamp, level, message }) => {
const formattedMessage =
truncateMaxLength === -1
? message
: _.truncate(message, { length: truncateMaxLength })

return `[${timestamp}] - [${level}] ${formattedMessage}`
})

/**
* Initialize logger.
*
* @param {number} logLevel - log level
* @param {number} truncate_max_limit - max limit to truncate log messages
*/
const initLogger = (logLevel = 0) => {
const initLogger = (
logLevel = 0,
truncate_max_limit = defaultLogMessageLength
) => {
if (global && global.paths && global.paths.FIREEDGE_LOG) {
const levelString = parseInt(logLevel, 10)
const logString = defaultLogsLevels && defaultLogsLevels[levelString]

const trans = []

const loggerFormat = format.combine(
format.timestamp(),
getTruncFormat(truncate_max_limit)
)

if (env && env.NODE_ENV && env.NODE_ENV === defaultWebpackMode) {
trans.push(
new transports.Console({
format: format.simple(),
format: loggerFormat,
})
)
} else {
Expand All @@ -49,7 +71,7 @@ const initLogger = (logLevel = 0) => {
level: logString,
filename: global.paths.FIREEDGE_LOG,
handleExceptions: true,
format: format.simple(),
format: loggerFormat,
maxsize: 5242880, // 5MB
colorize: false,
})
Expand All @@ -66,10 +88,11 @@ const initLogger = (logLevel = 0) => {
writeInLogger(message)
},
}

if (env && env.NODE_ENV && env.NODE_ENV === defaultWebpackMode) {
logger.clear().add(
new transports.Console({
format: format.simple(),
format: loggerFormat,
})
)
}
Expand Down

0 comments on commit b9d08e6

Please sign in to comment.