JavaScript 日志乱码通常不是 JS 本身的问题,而是字符编码不一致导致的。下面按常见场景给你处理方案:
确保 JS 文件是 UTF-8 无 BOM:
UTF-8<meta charset="UTF-8">
// 强制使用 UTF-8
process.env.LANG = 'en_US.UTF-8'
Windows CMD:
chcp 65001
console.log(Buffer.from('中文').toString('utf8'))
或写日志文件时指定编码:
fs.writeFileSync('log.txt', '中文日志', 'utf8')
确保:
Content-Type: application/json; charset=utf-8responseType 正确const winston = require('winston')
const logger = winston.createLogger({
transports: [
new winston.transports.File({
filename: 'app.log',
encoding: 'utf8'
})
]
})
如果你能贴一下:
我可以给你更精确的办法。