From 3b15a2e9de4073ec740b9a00b5018e21d56acbcc Mon Sep 17 00:00:00 2001 From: "Mr.zhang" Date: Thu, 11 Aug 2022 13:35:33 +0800 Subject: [PATCH] fix: windows7 os.hostname() throw error --- bole.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/bole.js b/bole.js index c33e090..2792e1c 100644 --- a/bole.js +++ b/bole.js @@ -4,12 +4,20 @@ const _stringify = require('fast-safe-stringify') const individual = require('individual')('$$bole', { fastTime: false }) // singleton const format = require('./format') const levels = 'debug info warn error'.split(' ') -const hostname = require('os').hostname() -const hostnameSt = _stringify(hostname) +const os = require('os') const pid = process.pid let hasObjMode = false const scache = [] +// Ref: https://github.com/rvagg/bole/issues/20 +let hostname +try { + hostname = os.hostname() +} catch (e) { + hostname = os.version().indexOf('Windows 7 ') === 0 ? 'windows7' : 'hostname-unknown' +} +const hostnameSt = _stringify(hostname) + for (const level of levels) { // prepare a common part of the stringified output scache[level] = ',"hostname":' + hostnameSt + ',"pid":' + pid + ',"level":"' + level