Skip to content

Commit

Permalink
lib/logging: Add logging time based on locale or configured timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Jun 7, 2018
1 parent 4988072 commit 522fd74
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"__README__": "https://github.com/sogehige/SogeBot/wiki/Install-and-Upgrade",
"timezone": "system",
"cpu": 4,
"settings": {
"__COMMENT__": "get bot oauth at http://oauth.sogehige.tv",
Expand Down
12 changes: 8 additions & 4 deletions libs/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ const format = winston.format
var fs = require('fs')
var _ = require('lodash')
var logDir = './logs'
var moment = require('moment')
var moment = require('moment-timezone')
const glob = require('glob')
const cluster = require('cluster')
const config = require('../config.json')

config.timezone = config.timezone === 'system' || _.isNil(config.timezone) ? moment.tz.guess() : config.timezone

if (!fs.existsSync(logDir)) fs.mkdirSync(logDir)

Expand Down Expand Up @@ -48,7 +51,6 @@ if (cluster.isWorker) {
levels: levels,
level: 'debug',
format: format.combine(
format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss.SSS'}),
format.printf(info => {
let level
if (info.level === 'error') level = '!!! ERROR !!!'
Expand All @@ -68,7 +70,9 @@ if (cluster.isWorker) {
if (info.level === 'sub') level = '+sub'
if (info.level === 'subgift') level = '+subgift'
if (info.level === 'resub') level = '+resub'
return `${info.timestamp} ${level} ${info.message} ${info.username ? `[${info.username}]` : ``}`

const timestamp = moment().tz(config.timezone).format('YYYY-MM-DD[T]HH:mm:ss.SSS')
return `${timestamp} ${level} ${info.message} ${info.username ? `[${info.username}]` : ``}`
})
),
exceptionHandlers: [
Expand Down Expand Up @@ -106,7 +110,7 @@ Logger.prototype.send = async function (self, socket, filters) {

Logger.prototype.doFilter = function (self, content) {
// remove startup, debug and errors
var sContent = content.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z? \| .*/g, '')
var sContent = content.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}? \| .*/g, '')
.replace(/.\[39m/g, '', '')
.split('\n')
content = []
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mathjs": "^4.3.0",
"moment": "^2.22.1",
"moment-precise-range-plugin": "^1.3.0",
"moment-timezone": "^0.5.17",
"mongodb": "^3.1.0-beta4",
"mongodb-uri": "^0.9.7",
"nedb": "^1.8.0",
Expand Down

0 comments on commit 522fd74

Please sign in to comment.