UNPKG

graphql-language-service-server

Version:
52 lines 1.82 kB
import * as fs from 'fs'; import * as os from 'os'; import { join } from 'path'; import { DIAGNOSTIC_SEVERITY, SEVERITY, } from 'graphql-language-service'; export class Logger { constructor(tmpDir, stderrOnly) { const dir = join(tmpDir || os.tmpdir(), 'graphql-language-service-logs'); try { if (!fs.existsSync(dir)) { fs.mkdirSync(dir); } } catch (_) { } this._logFilePath = join(dir, `graphql-language-service-log-${os.userInfo().username}-${getDateString()}.log`); this._stderrOnly = stderrOnly || false; } error(message) { this._log(message, SEVERITY.Error); } warn(message) { this._log(message, SEVERITY.Warning); } info(message) { this._log(message, SEVERITY.Information); } log(message) { this._log(message, SEVERITY.Hint); } _log(message, severityKey) { const timestamp = new Date().toLocaleString(undefined); const severity = DIAGNOSTIC_SEVERITY[severityKey]; const pid = process.pid; const stringMessage = String(message).trim(); const logMessage = `${timestamp} [${severity}] (pid: ${pid}) graphql-language-service-usage-logs: ${stringMessage}\n`; fs.appendFile(this._logFilePath, logMessage, _error => { }); this._getOutputStream(severity).write(logMessage, err => { err && console.error(err); }); } _getOutputStream(severity) { if (this._stderrOnly || severity === DIAGNOSTIC_SEVERITY.Error) { return process.stderr; } return process.stdout; } } function getDateString() { const date = new Date(); return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; } //# sourceMappingURL=Logger.js.map