From 0868c3517f8c2dda5527c8863ce5144fffde0eb5 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Feb 2019 18:16:57 +0900 Subject: [PATCH] Use internal logger instead of debug/console for logging Resolve #4104 Resolve #1966 Resolve #1937 --- package.json | 2 -- src/chart/index.ts | 2 +- src/daemons/server-stats.ts | 2 -- src/db/elasticsearch.ts | 7 ++++-- src/db/mongodb.ts | 3 +++ src/index.ts | 24 ++++++++++++------- src/misc/logger.ts | 7 ++++-- src/models/drive-file.ts | 4 ++-- src/models/favorite.ts | 4 ++-- src/models/note.ts | 10 ++++---- src/models/notification.ts | 6 ++--- src/models/user.ts | 4 ++-- src/push-sw.ts | 6 ++--- src/queue/index.ts | 3 +++ src/queue/processors/http/deliver.ts | 5 ++-- src/queue/processors/http/index.ts | 3 ++- src/queue/processors/http/process-inbox.ts | 19 +++++++-------- src/remote/activitypub/kernel/accept/index.ts | 11 ++++----- .../activitypub/kernel/announce/index.ts | 11 ++++----- .../activitypub/kernel/announce/note.ts | 7 +++--- src/remote/activitypub/kernel/block/index.ts | 6 ++--- src/remote/activitypub/kernel/create/index.ts | 11 ++++----- src/remote/activitypub/kernel/delete/index.ts | 3 ++- src/remote/activitypub/kernel/delete/note.ts | 7 +++--- src/remote/activitypub/kernel/index.ts | 7 +++--- src/remote/activitypub/kernel/reject/index.ts | 11 ++++----- src/remote/activitypub/kernel/undo/block.ts | 6 ++--- src/remote/activitypub/kernel/undo/index.ts | 9 ++++--- .../api/endpoints/drive/files/create.ts | 3 ++- src/server/api/endpoints/i/update_email.ts | 6 +++-- src/server/api/endpoints/notes/search.ts | 3 ++- .../api/endpoints/users/recommendation.ts | 3 ++- src/server/api/endpoints/users/show.ts | 3 ++- src/server/api/logger.ts | 3 +++ src/server/file/send-drive-file.ts | 3 ++- src/server/index.ts | 4 ++-- src/services/blocking/delete.ts | 5 +++- src/services/following/delete.ts | 5 +++- src/services/following/requests/accept.ts | 5 +++- src/services/note/create.ts | 1 - src/services/note/reaction/create.ts | 1 - 41 files changed, 136 insertions(+), 109 deletions(-) create mode 100644 src/server/api/logger.ts diff --git a/package.json b/package.json index 0d5ce5a0cd..e0886f58cb 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,6 @@ "@types/bcryptjs": "2.4.2", "@types/chai-http": "3.0.5", "@types/dateformat": "3.0.0", - "@types/debug": "0.0.31", "@types/deep-equal": "1.0.1", "@types/double-ended-queue": "2.1.0", "@types/elasticsearch": "5.0.30", @@ -108,7 +107,6 @@ "css-loader": "1.0.1", "cssnano": "4.1.8", "dateformat": "3.0.3", - "debug": "4.1.1", "deep-equal": "1.0.1", "deepcopy": "0.6.3", "diskusage": "1.0.0", diff --git a/src/chart/index.ts b/src/chart/index.ts index edf773d9ca..5c1956a440 100644 --- a/src/chart/index.ts +++ b/src/chart/index.ts @@ -181,7 +181,7 @@ export default abstract class Chart { if (e.code === 11000) { log = await this.getLatestLog(span, group); } else { - console.error(e); + logger.error(e); throw e; } } diff --git a/src/daemons/server-stats.ts b/src/daemons/server-stats.ts index f3e3d61f8b..6028277b25 100644 --- a/src/daemons/server-stats.ts +++ b/src/daemons/server-stats.ts @@ -60,7 +60,6 @@ async function usedMem() { const data = await sysUtils.mem(); return data.active; } catch (error) { - console.error(error); throw error; } } @@ -71,7 +70,6 @@ async function totalMem() { const data = await sysUtils.mem(); return data.total; } catch (error) { - console.error(error); throw error; } } diff --git a/src/db/elasticsearch.ts b/src/db/elasticsearch.ts index ee5769d1d4..cbe6afbbb9 100644 --- a/src/db/elasticsearch.ts +++ b/src/db/elasticsearch.ts @@ -1,5 +1,8 @@ import * as elasticsearch from 'elasticsearch'; import config from '../config'; +import Logger from '../misc/logger'; + +const esLogger = new Logger('es'); const index = { settings: { @@ -50,9 +53,9 @@ if (client) { requestTimeout: 30000 }, error => { if (error) { - console.error('elasticsearch is down!'); + esLogger.error('elasticsearch is down!'); } else { - console.log('elasticsearch is available!'); + esLogger.succ('elasticsearch is available!'); } }); diff --git a/src/db/mongodb.ts b/src/db/mongodb.ts index f82ced1765..dedb289ce9 100644 --- a/src/db/mongodb.ts +++ b/src/db/mongodb.ts @@ -18,6 +18,7 @@ export default db; * MongoDB native module (officialy) */ import * as mongodb from 'mongodb'; +import Logger from '../misc/logger'; let mdb: mongodb.Db; @@ -37,3 +38,5 @@ const nativeDbConn = async (): Promise => { }; export { nativeDbConn }; + +export const dbLogger = new Logger('db'); diff --git a/src/index.ts b/src/index.ts index a42eec1a6f..af9f7de983 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,7 @@ program .version(pkg.version) .option('--no-daemons', 'Disable daemon processes (for debbuging)') .option('--disable-clustering', 'Disable clustering') + .option('--quiet', 'Suppress all logs') .parse(process.argv); //#endregion @@ -67,11 +68,15 @@ function main() { async function masterMain() { let config: Config; - console.log(' _____ _ _ '); - console.log('| |_|___ ___| |_ ___ _ _ '); - console.log('| | | | |_ -|_ -| \'_| -_| | |'); - console.log('|_|_|_|_|___|___|_,_|___|_ |'); - console.log(' |___|\n'); + if (!program.quiet) { + //#region Misskey logo + console.log(' _____ _ _ '); + console.log('| |_|___ ___| |_ ___ _ _ '); + console.log('| | | | |_ -|_ -| \'_| -_| | |'); + console.log('|_|_|_|_|___|___|_,_|___|_ |'); + console.log(' |___|\n'); + //#endregion + } bootLogger.info('Welcome to Misskey!'); bootLogger.info(`Misskey v${pkg.version}`, true); @@ -80,8 +85,7 @@ async function masterMain() { // initialize app config = await init(); } catch (e) { - console.error(e); - bootLogger.error('Fatal error occurred during initialization'); + bootLogger.error('Fatal error occurred during initialization', true); process.exit(1); } @@ -191,7 +195,7 @@ async function init(): Promise { } if (!await isPortAvailable(config.port)) { - bootLogger.error(`Port ${config.port} is already in use`); + bootLogger.error(`Port ${config.port} is already in use`, true); process.exit(1); } @@ -263,7 +267,9 @@ cluster.on('exit', worker => { }); // Display detail of unhandled promise rejection -process.on('unhandledRejection', console.dir); +if (!program.quiet) { + process.on('unhandledRejection', console.dir); +} // Display detail of uncaught exception process.on('uncaughtException', err => { diff --git a/src/misc/logger.ts b/src/misc/logger.ts index 8fbff9638d..6d75e65ce4 100644 --- a/src/misc/logger.ts +++ b/src/misc/logger.ts @@ -2,6 +2,8 @@ import * as cluster from 'cluster'; import chalk from 'chalk'; import * as dateformat from 'dateformat'; +const quiet = process.argv.find(x => x == '--quiet'); + export default class Logger { private domain: string; private color?: string; @@ -19,6 +21,7 @@ export default class Logger { } public log(level: string, message: string, important = false): void { + if (quiet) return; const domain = this.color ? chalk.keyword(this.color)(this.domain) : chalk.white(this.domain); if (this.parentLogger) { this.parentLogger.log(level, `[${domain}]\t${message}`, important); @@ -30,8 +33,8 @@ export default class Logger { } } - public error(message: string | Error): void { // 実行を継続できない状況で使う - this.log(chalk.red('ERR '), chalk.red(message.toString())); + public error(message: string | Error, important = false): void { // 実行を継続できない状況で使う + this.log(chalk.red('ERR '), chalk.red(message.toString()), important); } public warn(message: string, important = false): void { // 実行を継続できるが改善すべき状況で使う diff --git a/src/models/drive-file.ts b/src/models/drive-file.ts index b34ba7ded8..62a544c214 100644 --- a/src/models/drive-file.ts +++ b/src/models/drive-file.ts @@ -2,7 +2,7 @@ import * as mongo from 'mongodb'; import * as deepcopy from 'deepcopy'; import { pack as packFolder } from './drive-folder'; import { pack as packUser } from './user'; -import monkDb, { nativeDbConn } from '../db/mongodb'; +import monkDb, { nativeDbConn, dbLogger } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; import getDriveFileUrl, { getOriginalUrl } from '../misc/get-drive-file-url'; @@ -171,7 +171,7 @@ export const pack = ( // (データベースの欠損などで)ファイルがデータベース上に見つからなかったとき if (_file == null) { - console.warn(`[DAMAGED DB] (missing) pkg: driveFile :: ${file}`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: driveFile :: ${file}`); return resolve(null); } diff --git a/src/models/favorite.ts b/src/models/favorite.ts index 0a8fec020f..e3aa92c887 100644 --- a/src/models/favorite.ts +++ b/src/models/favorite.ts @@ -1,6 +1,6 @@ import * as mongo from 'mongodb'; import * as deepcopy from 'deepcopy'; -import db from '../db/mongodb'; +import db, { dbLogger } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; import { pack as packNote } from './note'; @@ -56,7 +56,7 @@ export const pack = ( // (データベースの不具合などで)投稿が見つからなかったら if (_favorite.note == null) { - console.warn(`[DAMAGED DB] (missing) pkg: favorite -> note :: ${_favorite.id} (note ${_favorite.noteId})`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: favorite -> note :: ${_favorite.id} (note ${_favorite.noteId})`); return resolve(null); } diff --git a/src/models/note.ts b/src/models/note.ts index a887cf1c84..b3d5ed6977 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -1,7 +1,7 @@ import * as mongo from 'mongodb'; import * as deepcopy from 'deepcopy'; import rap from '@prezzemolo/rap'; -import db from '../db/mongodb'; +import db, { dbLogger } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; import { length } from 'stringz'; import { IUser, pack as packUser } from './user'; @@ -231,7 +231,7 @@ export const pack = async ( // (データベースの欠損などで)投稿がデータベース上に見つからなかったとき if (_note == null) { - console.warn(`[DAMAGED DB] (missing) pkg: note :: ${note}`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: note :: ${note}`); return null; } @@ -367,18 +367,18 @@ export const pack = async ( //#region (データベースの欠損などで)参照しているデータがデータベース上に見つからなかったとき if (_note.user == null) { - console.warn(`[DAMAGED DB] (missing) pkg: note -> user :: ${_note.id} (user ${_note.userId})`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> user :: ${_note.id} (user ${_note.userId})`); return null; } if (opts.detail) { if (_note.replyId != null && _note.reply == null) { - console.warn(`[DAMAGED DB] (missing) pkg: note -> reply :: ${_note.id} (reply ${_note.replyId})`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> reply :: ${_note.id} (reply ${_note.replyId})`); return null; } if (_note.renoteId != null && _note.renote == null) { - console.warn(`[DAMAGED DB] (missing) pkg: note -> renote :: ${_note.id} (renote ${_note.renoteId})`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: note -> renote :: ${_note.id} (renote ${_note.renoteId})`); return null; } } diff --git a/src/models/notification.ts b/src/models/notification.ts index d49494391e..aedeafb522 100644 --- a/src/models/notification.ts +++ b/src/models/notification.ts @@ -1,6 +1,6 @@ import * as mongo from 'mongodb'; import * as deepcopy from 'deepcopy'; -import db from '../db/mongodb'; +import db, { dbLogger } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; import { IUser, pack as packUser } from './user'; import { pack as packNote } from './note'; @@ -106,12 +106,12 @@ export const pack = (notification: any) => new Promise(async (resolve, reje // (データベースの不具合などで)投稿が見つからなかったら if (_notification.note == null) { - console.warn(`[DAMAGED DB] (missing) pkg: notification -> note :: ${_notification.id} (note ${_notification.noteId})`); + dbLogger.warn(`[DAMAGED DB] (missing) pkg: notification -> note :: ${_notification.id} (note ${_notification.noteId})`); return resolve(null); } break; default: - console.error(`Unknown type: ${_notification.type}`); + dbLogger.error(`Unknown type: ${_notification.type}`); break; } diff --git a/src/models/user.ts b/src/models/user.ts index 66f515ddab..2453a2ed15 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -1,7 +1,7 @@ import * as mongo from 'mongodb'; import * as deepcopy from 'deepcopy'; import rap from '@prezzemolo/rap'; -import db from '../db/mongodb'; +import db, { dbLogger } from '../db/mongodb'; import isObjectId from '../misc/is-objectid'; import { packMany as packNoteMany } from './note'; import Following from './following'; @@ -286,7 +286,7 @@ export const pack = ( // (データベースの欠損などで)ユーザーがデータベース上に見つからなかったとき if (_user == null) { - console.warn(`user not found on database: ${user}`); + dbLogger.warn(`user not found on database: ${user}`); return resolve(null); } diff --git a/src/push-sw.ts b/src/push-sw.ts index 07b7508459..8fd2dc137e 100644 --- a/src/push-sw.ts +++ b/src/push-sw.ts @@ -44,9 +44,9 @@ export default async function(userId: mongo.ObjectID | string, type: string, bod push.sendNotification(pushSubscription, JSON.stringify({ type, body })).catch((err: any) => { - //console.log(err.statusCode); - //console.log(err.headers); - //console.log(err.body); + //swLogger.info(err.statusCode); + //swLogger.info(err.headers); + //swLogger.info(err.body); if (err.statusCode == 410) { Subscription.remove({ diff --git a/src/queue/index.ts b/src/queue/index.ts index 8683bcd1df..431d4cb3e5 100644 --- a/src/queue/index.ts +++ b/src/queue/index.ts @@ -1,5 +1,6 @@ import http from './processors/http'; import { ILocalUser } from '../models/user'; +import Logger from '../misc/logger'; export function createHttpJob(data: any) { return http({ data }, () => {}); @@ -15,3 +16,5 @@ export function deliver(user: ILocalUser, content: any, to: any) { to }); } + +export const queueLogger = new Logger('queue'); diff --git a/src/queue/processors/http/deliver.ts b/src/queue/processors/http/deliver.ts index 621219fec6..d8d90a2773 100644 --- a/src/queue/processors/http/deliver.ts +++ b/src/queue/processors/http/deliver.ts @@ -1,6 +1,7 @@ import * as bq from 'bee-queue'; import request from '../../../remote/activitypub/request'; +import { queueLogger } from '../..'; export default async (job: bq.Job, done: any): Promise => { try { @@ -13,11 +14,11 @@ export default async (job: bq.Job, done: any): Promise => { // 何回再送しても成功することはないということなのでエラーにはしないでおく done(); } else { - console.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`); + queueLogger.warn(`deliver failed: ${res.statusCode} ${res.statusMessage} to=${job.data.to}`); done(res.statusMessage); } } else { - console.warn(`deliver failed: ${res} to=${job.data.to}`); + queueLogger.warn(`deliver failed: ${res} to=${job.data.to}`); done(); } } diff --git a/src/queue/processors/http/index.ts b/src/queue/processors/http/index.ts index 156be42250..74ed723bd3 100644 --- a/src/queue/processors/http/index.ts +++ b/src/queue/processors/http/index.ts @@ -1,5 +1,6 @@ import deliver from './deliver'; import processInbox from './process-inbox'; +import { queueLogger } from '../..'; const handlers: any = { deliver, @@ -12,7 +13,7 @@ export default (job: any, done: any) => { if (handler) { handler(job, done); } else { - console.error(`Unknown job: ${job.data.type}`); + queueLogger.error(`Unknown job: ${job.data.type}`); done(); } }; diff --git a/src/queue/processors/http/process-inbox.ts b/src/queue/processors/http/process-inbox.ts index 32ac7aebd7..5b6d0bcd76 100644 --- a/src/queue/processors/http/process-inbox.ts +++ b/src/queue/processors/http/process-inbox.ts @@ -1,6 +1,4 @@ import * as bq from 'bee-queue'; -import * as debug from 'debug'; - import * as httpSignature from 'http-signature'; import parseAcct from '../../../misc/acct/parse'; import User, { IRemoteUser } from '../../../models/user'; @@ -9,8 +7,9 @@ import { resolvePerson, updatePerson } from '../../../remote/activitypub/models/ import { toUnicode } from 'punycode'; import { URL } from 'url'; import { publishApLogStream } from '../../../stream'; +import Logger from '../../../misc/logger'; -const log = debug('misskey:queue:inbox'); +const logger = new Logger('inbox'); // ユーザーのinboxにアクティビティが届いた時の処理 export default async (job: bq.Job, done: any): Promise => { @@ -21,7 +20,7 @@ export default async (job: bq.Job, done: any): Promise => { const info = Object.assign({}, activity); delete info['@context']; delete info['signature']; - log(info); + logger.info(info); //#endregion const keyIdLower = signature.keyId.toLowerCase(); @@ -30,7 +29,7 @@ export default async (job: bq.Job, done: any): Promise => { if (keyIdLower.startsWith('acct:')) { const { username, host } = parseAcct(keyIdLower.slice('acct:'.length)); if (host === null) { - console.warn(`request was made by local user: @${username}`); + logger.warn(`request was made by local user: @${username}`); done(); return; } @@ -39,7 +38,7 @@ export default async (job: bq.Job, done: any): Promise => { try { ValidateActivity(activity, host); } catch (e) { - console.warn(e.message); + logger.warn(e.message); done(); return; } @@ -51,7 +50,7 @@ export default async (job: bq.Job, done: any): Promise => { try { ValidateActivity(activity, host); } catch (e) { - console.warn(e.message); + logger.warn(e.message); done(); return; } @@ -66,9 +65,9 @@ export default async (job: bq.Job, done: any): Promise => { if (activity.type === 'Update') { if (activity.object && activity.object.type === 'Person') { if (user == null) { - console.warn('Update activity received, but user not registed.'); + logger.warn('Update activity received, but user not registed.'); } else if (!httpSignature.verifySignature(signature, user.publicKey.publicKeyPem)) { - console.warn('Update activity received, but signature verification failed.'); + logger.warn('Update activity received, but signature verification failed.'); } else { updatePerson(activity.actor, null, activity.object); } @@ -88,7 +87,7 @@ export default async (job: bq.Job, done: any): Promise => { } if (!httpSignature.verifySignature(signature, user.publicKey.publicKeyPem)) { - console.warn('signature verification failed'); + logger.error('signature verification failed'); done(); return; } diff --git a/src/remote/activitypub/kernel/accept/index.ts b/src/remote/activitypub/kernel/accept/index.ts index 572a254d9a..443c1935d6 100644 --- a/src/remote/activitypub/kernel/accept/index.ts +++ b/src/remote/activitypub/kernel/accept/index.ts @@ -1,16 +1,15 @@ -import * as debug from 'debug'; - import Resolver from '../../resolver'; import { IRemoteUser } from '../../../../models/user'; import acceptFollow from './follow'; import { IAccept, IFollow } from '../../type'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IAccept): Promise => { const uri = activity.id || activity; - log(`Accept: ${uri}`); + logger.info(`Accept: ${uri}`); const resolver = new Resolver(); @@ -19,7 +18,7 @@ export default async (actor: IRemoteUser, activity: IAccept): Promise => { try { object = await resolver.resolve(activity.object); } catch (e) { - log(`Resolution failed: ${e}`); + logger.error(`Resolution failed: ${e}`); throw e; } @@ -29,7 +28,7 @@ export default async (actor: IRemoteUser, activity: IAccept): Promise => { break; default: - console.warn(`Unknown accept type: ${object.type}`); + logger.warn(`Unknown accept type: ${object.type}`); break; } }; diff --git a/src/remote/activitypub/kernel/announce/index.ts b/src/remote/activitypub/kernel/announce/index.ts index 612b5dee84..80875b90da 100644 --- a/src/remote/activitypub/kernel/announce/index.ts +++ b/src/remote/activitypub/kernel/announce/index.ts @@ -1,16 +1,15 @@ -import * as debug from 'debug'; - import Resolver from '../../resolver'; import { IRemoteUser } from '../../../../models/user'; import announceNote from './note'; import { IAnnounce, INote } from '../../type'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IAnnounce): Promise => { const uri = activity.id || activity; - log(`Announce: ${uri}`); + logger.info(`Announce: ${uri}`); const resolver = new Resolver(); @@ -19,7 +18,7 @@ export default async (actor: IRemoteUser, activity: IAnnounce): Promise => try { object = await resolver.resolve(activity.object); } catch (e) { - log(`Resolution failed: ${e}`); + logger.error(`Resolution failed: ${e}`); throw e; } @@ -29,7 +28,7 @@ export default async (actor: IRemoteUser, activity: IAnnounce): Promise => break; default: - console.warn(`Unknown announce type: ${object.type}`); + logger.warn(`Unknown announce type: ${object.type}`); break; } }; diff --git a/src/remote/activitypub/kernel/announce/note.ts b/src/remote/activitypub/kernel/announce/note.ts index 5eaeaf7918..128ca6da81 100644 --- a/src/remote/activitypub/kernel/announce/note.ts +++ b/src/remote/activitypub/kernel/announce/note.ts @@ -1,13 +1,12 @@ -import * as debug from 'debug'; - import Resolver from '../../resolver'; import post from '../../../../services/note/create'; import { IRemoteUser, IUser } from '../../../../models/user'; import { IAnnounce, INote } from '../../type'; import { fetchNote, resolveNote } from '../../models/note'; import { resolvePerson } from '../../models/person'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; /** * アナウンスアクティビティを捌きます @@ -32,7 +31,7 @@ export default async function(resolver: Resolver, actor: IRemoteUser, activity: const renote = await resolveNote(note); - log(`Creating the (Re)Note: ${uri}`); + logger.info(`Creating the (Re)Note: ${uri}`); //#region Visibility const visibility = getVisibility(activity.to, activity.cc, actor); diff --git a/src/remote/activitypub/kernel/block/index.ts b/src/remote/activitypub/kernel/block/index.ts index dec591accf..a10163016c 100644 --- a/src/remote/activitypub/kernel/block/index.ts +++ b/src/remote/activitypub/kernel/block/index.ts @@ -1,18 +1,18 @@ import * as mongo from 'mongodb'; import User, { IRemoteUser } from '../../../../models/user'; import config from '../../../../config'; -import * as debug from 'debug'; import { IBlock } from '../../type'; import block from '../../../../services/blocking/create'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IBlock): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; const uri = activity.id || activity; - log(`Block: ${uri}`); + logger.info(`Block: ${uri}`); if (!id.startsWith(config.url + '/')) { return null; diff --git a/src/remote/activitypub/kernel/create/index.ts b/src/remote/activitypub/kernel/create/index.ts index e11bcac811..c633d95487 100644 --- a/src/remote/activitypub/kernel/create/index.ts +++ b/src/remote/activitypub/kernel/create/index.ts @@ -1,17 +1,16 @@ -import * as debug from 'debug'; - import Resolver from '../../resolver'; import { IRemoteUser } from '../../../../models/user'; import createNote from './note'; import createImage from './image'; import { ICreate } from '../../type'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: ICreate): Promise => { const uri = activity.id || activity; - log(`Create: ${uri}`); + logger.info(`Create: ${uri}`); const resolver = new Resolver(); @@ -20,7 +19,7 @@ export default async (actor: IRemoteUser, activity: ICreate): Promise => { try { object = await resolver.resolve(activity.object); } catch (e) { - log(`Resolution failed: ${e}`); + logger.error(`Resolution failed: ${e}`); throw e; } @@ -34,7 +33,7 @@ export default async (actor: IRemoteUser, activity: ICreate): Promise => { break; default: - console.warn(`Unknown type: ${object.type}`); + logger.warn(`Unknown type: ${object.type}`); break; } }; diff --git a/src/remote/activitypub/kernel/delete/index.ts b/src/remote/activitypub/kernel/delete/index.ts index 5d7a23584f..eead34785c 100644 --- a/src/remote/activitypub/kernel/delete/index.ts +++ b/src/remote/activitypub/kernel/delete/index.ts @@ -3,6 +3,7 @@ import deleteNote from './note'; import Note from '../../../../models/note'; import { IRemoteUser } from '../../../../models/user'; import { IDelete } from '../../type'; +import { apLogger } from '../../logger'; /** * 削除アクティビティを捌きます @@ -31,7 +32,7 @@ export default async (actor: IRemoteUser, activity: IDelete): Promise => { break; default: - console.warn(`Unknown type: ${object.type}`); + apLogger.warn(`Unknown type: ${object.type}`); break; } }; diff --git a/src/remote/activitypub/kernel/delete/note.ts b/src/remote/activitypub/kernel/delete/note.ts index 1951982f69..f67919c56b 100644 --- a/src/remote/activitypub/kernel/delete/note.ts +++ b/src/remote/activitypub/kernel/delete/note.ts @@ -1,13 +1,12 @@ -import * as debug from 'debug'; - import Note from '../../../../models/note'; import { IRemoteUser } from '../../../../models/user'; import deleteNode from '../../../../services/note/delete'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async function(actor: IRemoteUser, uri: string): Promise { - log(`Deleting the Note: ${uri}`); + logger.info(`Deleting the Note: ${uri}`); const note = await Note.findOne({ uri }); diff --git a/src/remote/activitypub/kernel/index.ts b/src/remote/activitypub/kernel/index.ts index 61bb89f5e9..7cf9ba8334 100644 --- a/src/remote/activitypub/kernel/index.ts +++ b/src/remote/activitypub/kernel/index.ts @@ -11,6 +11,7 @@ import reject from './reject'; import add from './add'; import remove from './remove'; import block from './block'; +import { apLogger } from '../logger'; const self = async (actor: IRemoteUser, activity: Object): Promise => { switch (activity.type) { @@ -35,11 +36,11 @@ const self = async (actor: IRemoteUser, activity: Object): Promise => { break; case 'Add': - await add(actor, activity).catch(err => console.log(err)); + await add(actor, activity).catch(err => apLogger.error(err)); break; case 'Remove': - await remove(actor, activity).catch(err => console.log(err)); + await remove(actor, activity).catch(err => apLogger.error(err)); break; case 'Announce': @@ -64,7 +65,7 @@ const self = async (actor: IRemoteUser, activity: Object): Promise => { break; default: - console.warn(`unknown activity type: ${(activity as any).type}`); + apLogger.warn(`unknown activity type: ${(activity as any).type}`); return null; } }; diff --git a/src/remote/activitypub/kernel/reject/index.ts b/src/remote/activitypub/kernel/reject/index.ts index d89985c238..c3585abbb6 100644 --- a/src/remote/activitypub/kernel/reject/index.ts +++ b/src/remote/activitypub/kernel/reject/index.ts @@ -1,16 +1,15 @@ -import * as debug from 'debug'; - import Resolver from '../../resolver'; import { IRemoteUser } from '../../../../models/user'; import rejectFollow from './follow'; import { IReject, IFollow } from '../../type'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IReject): Promise => { const uri = activity.id || activity; - log(`Reject: ${uri}`); + logger.info(`Reject: ${uri}`); const resolver = new Resolver(); @@ -19,7 +18,7 @@ export default async (actor: IRemoteUser, activity: IReject): Promise => { try { object = await resolver.resolve(activity.object); } catch (e) { - log(`Resolution failed: ${e}`); + logger.error(`Resolution failed: ${e}`); throw e; } @@ -29,7 +28,7 @@ export default async (actor: IRemoteUser, activity: IReject): Promise => { break; default: - console.warn(`Unknown reject type: ${object.type}`); + logger.warn(`Unknown reject type: ${object.type}`); break; } }; diff --git a/src/remote/activitypub/kernel/undo/block.ts b/src/remote/activitypub/kernel/undo/block.ts index b735f114d0..4a22ac7924 100644 --- a/src/remote/activitypub/kernel/undo/block.ts +++ b/src/remote/activitypub/kernel/undo/block.ts @@ -1,18 +1,18 @@ import * as mongo from 'mongodb'; import User, { IRemoteUser } from '../../../../models/user'; import config from '../../../../config'; -import * as debug from 'debug'; import { IBlock } from '../../type'; import unblock from '../../../../services/blocking/delete'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IBlock): Promise => { const id = typeof activity.object == 'string' ? activity.object : activity.object.id; const uri = activity.id || activity; - log(`UnBlock: ${uri}`); + logger.info(`UnBlock: ${uri}`); if (!id.startsWith(config.url + '/')) { return null; diff --git a/src/remote/activitypub/kernel/undo/index.ts b/src/remote/activitypub/kernel/undo/index.ts index 471988f052..80b44fae04 100644 --- a/src/remote/activitypub/kernel/undo/index.ts +++ b/src/remote/activitypub/kernel/undo/index.ts @@ -1,13 +1,12 @@ -import * as debug from 'debug'; - import { IRemoteUser } from '../../../../models/user'; import { IUndo, IFollow, IBlock, ILike } from '../../type'; import unfollow from './follow'; import unblock from './block'; import undoLike from './like'; import Resolver from '../../resolver'; +import { apLogger } from '../../logger'; -const log = debug('misskey:activitypub'); +const logger = apLogger; export default async (actor: IRemoteUser, activity: IUndo): Promise => { if ('actor' in activity && actor.uri !== activity.actor) { @@ -16,7 +15,7 @@ export default async (actor: IRemoteUser, activity: IUndo): Promise => { const uri = activity.id || activity; - log(`Undo: ${uri}`); + logger.info(`Undo: ${uri}`); const resolver = new Resolver(); @@ -25,7 +24,7 @@ export default async (actor: IRemoteUser, activity: IUndo): Promise => { try { object = await resolver.resolve(activity.object); } catch (e) { - log(`Resolution failed: ${e}`); + logger.error(`Resolution failed: ${e}`); throw e; } diff --git a/src/server/api/endpoints/drive/files/create.ts b/src/server/api/endpoints/drive/files/create.ts index ce93202991..47a407eff5 100644 --- a/src/server/api/endpoints/drive/files/create.ts +++ b/src/server/api/endpoints/drive/files/create.ts @@ -3,6 +3,7 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id import { validateFileName, pack } from '../../../../../models/drive-file'; import create from '../../../../../services/drive/add-file'; import define from '../../../define'; +import { apiLogger } from '../../../logger'; export const meta = { desc: { @@ -76,7 +77,7 @@ export default define(meta, (ps, user, app, file, cleanup) => new Promise(async res(pack(driveFile, { self: true })); } catch (e) { - console.error(e); + apiLogger.error(e); cleanup(); diff --git a/src/server/api/endpoints/i/update_email.ts b/src/server/api/endpoints/i/update_email.ts index aeaa3d39e4..eeac460796 100644 --- a/src/server/api/endpoints/i/update_email.ts +++ b/src/server/api/endpoints/i/update_email.ts @@ -8,6 +8,7 @@ import rndstr from 'rndstr'; import config from '../../../../config'; import * as ms from 'ms'; import * as bcrypt from 'bcryptjs'; +import { apiLogger } from '../../logger'; export const meta = { requireCredential: true, @@ -91,10 +92,11 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => { text: `To verify email, please click this link: ${link}` }, (error, info) => { if (error) { - return console.error(error); + apiLogger.error(error); + return; } - console.log('Message sent: %s', info.messageId); + apiLogger.info('Message sent: %s', info.messageId); }); } })); diff --git a/src/server/api/endpoints/notes/search.ts b/src/server/api/endpoints/notes/search.ts index e107ca4e15..652dc6b31d 100644 --- a/src/server/api/endpoints/notes/search.ts +++ b/src/server/api/endpoints/notes/search.ts @@ -4,6 +4,7 @@ import Note from '../../../../models/note'; import { packMany } from '../../../../models/note'; import es from '../../../../db/elasticsearch'; import define from '../../define'; +import { apiLogger } from '../../logger'; export const meta = { desc: { @@ -52,7 +53,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => { } }, async (error, response) => { if (error) { - console.error(error); + apiLogger.error(error); return res(500); } diff --git a/src/server/api/endpoints/users/recommendation.ts b/src/server/api/endpoints/users/recommendation.ts index 0d7e57b529..e3a03888b6 100644 --- a/src/server/api/endpoints/users/recommendation.ts +++ b/src/server/api/endpoints/users/recommendation.ts @@ -8,6 +8,7 @@ import define from '../../define'; import fetchMeta from '../../../../misc/fetch-meta'; import resolveUser from '../../../../remote/resolve-user'; import { getHideUserIds } from '../../common/get-hide-users'; +import { apiLogger } from '../../logger'; export const meta = { desc: { @@ -102,7 +103,7 @@ async function convertUsers(src: IRecommendUser[], me: ILocalUser) { const packed = await Promise.all(src.map(async x => { const user = await resolveUser(x.username, x.host) .catch(() => { - console.warn(`Can't resolve ${x.username}@${x.host}`); + apiLogger.warn(`Can't resolve ${x.username}@${x.host}`); return null; }); diff --git a/src/server/api/endpoints/users/show.ts b/src/server/api/endpoints/users/show.ts index fd26554709..83897dd45c 100644 --- a/src/server/api/endpoints/users/show.ts +++ b/src/server/api/endpoints/users/show.ts @@ -2,6 +2,7 @@ import $ from 'cafy'; import ID, { transform, transformMany } from '../../../../ import User, { pack, isRemoteUser } from '../../../../models/user'; import resolveRemoteUser from '../../../../remote/resolve-user'; import define from '../../define'; +import { apiLogger } from '../../logger'; const cursorOption = { fields: { data: false } }; @@ -59,7 +60,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => { try { user = await resolveRemoteUser(ps.username, ps.host, cursorOption); } catch (e) { - console.warn(`failed to resolve remote user: ${e}`); + apiLogger.warn(`failed to resolve remote user: ${e}`); return rej('failed to resolve remote user'); } } else { diff --git a/src/server/api/logger.ts b/src/server/api/logger.ts new file mode 100644 index 0000000000..334a696331 --- /dev/null +++ b/src/server/api/logger.ts @@ -0,0 +1,3 @@ +import Logger from "../../misc/logger"; + +export const apiLogger = new Logger('api'); diff --git a/src/server/file/send-drive-file.ts b/src/server/file/send-drive-file.ts index 1cfdd1c4d5..691d3bf848 100644 --- a/src/server/file/send-drive-file.ts +++ b/src/server/file/send-drive-file.ts @@ -4,11 +4,12 @@ import * as mongodb from 'mongodb'; import DriveFile, { getDriveFileBucket } from '../../models/drive-file'; import DriveFileThumbnail, { getDriveFileThumbnailBucket } from '../../models/drive-file-thumbnail'; import DriveFileWebpublic, { getDriveFileWebpublicBucket } from '../../models/drive-file-webpublic'; +import { serverLogger } from '..'; const assets = `${__dirname}/../../server/file/assets/`; const commonReadableHandlerGenerator = (ctx: Koa.BaseContext) => (e: Error): void => { - console.error(e); + serverLogger.error(e); ctx.status = 500; }; diff --git a/src/server/index.ts b/src/server/index.ts index 012f080f98..57f1326f2f 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -24,7 +24,7 @@ import { sum } from '../prelude/array'; import User from '../models/user'; import Logger from '../misc/logger'; -const logger = new Logger('server', 'gray'); +export const serverLogger = new Logger('server', 'gray'); // Init app const app = new Koa(); @@ -33,7 +33,7 @@ app.proxy = true; if (!['production', 'test'].includes(process.env.NODE_ENV)) { // Logger app.use(koaLogger(str => { - logger.info(str); + serverLogger.info(str); })); // Delay diff --git a/src/services/blocking/delete.ts b/src/services/blocking/delete.ts index 113b4ae458..425648f4cc 100644 --- a/src/services/blocking/delete.ts +++ b/src/services/blocking/delete.ts @@ -4,6 +4,9 @@ import { renderActivity } from '../../remote/activitypub/renderer'; import renderBlock from '../../remote/activitypub/renderer/block'; import renderUndo from '../../remote/activitypub/renderer/undo'; import { deliver } from '../../queue'; +import Logger from '../../misc/logger'; + +const logger = new Logger('blocking/delete'); export default async function(blocker: IUser, blockee: IUser) { const blocking = await Blocking.findOne({ @@ -12,7 +15,7 @@ export default async function(blocker: IUser, blockee: IUser) { }); if (blocking == null) { - console.warn('ブロック解除がリクエストされましたがブロックしていませんでした'); + logger.warn('ブロック解除がリクエストされましたがブロックしていませんでした'); return; } diff --git a/src/services/following/delete.ts b/src/services/following/delete.ts index ca5bc0912d..1c5e8272f2 100644 --- a/src/services/following/delete.ts +++ b/src/services/following/delete.ts @@ -6,6 +6,9 @@ import renderFollow from '../../remote/activitypub/renderer/follow'; import renderUndo from '../../remote/activitypub/renderer/undo'; import { deliver } from '../../queue'; import perUserFollowingChart from '../../chart/per-user-following'; +import Logger from '../../misc/logger'; + +const logger = new Logger('following/delete'); export default async function(follower: IUser, followee: IUser) { const following = await Following.findOne({ @@ -14,7 +17,7 @@ export default async function(follower: IUser, followee: IUser) { }); if (following == null) { - console.warn('フォロー解除がリクエストされましたがフォローしていませんでした'); + logger.warn('フォロー解除がリクエストされましたがフォローしていませんでした'); return; } diff --git a/src/services/following/requests/accept.ts b/src/services/following/requests/accept.ts index dce2838405..76bb67afa1 100644 --- a/src/services/following/requests/accept.ts +++ b/src/services/following/requests/accept.ts @@ -7,6 +7,9 @@ import { deliver } from '../../../queue'; import Following from '../../../models/following'; import { publishMainStream } from '../../../stream'; import perUserFollowingChart from '../../../chart/per-user-following'; +import Logger from '../../../misc/logger'; + +const logger = new Logger('following/requests/accept'); export default async function(followee: IUser, follower: IUser) { let incremented = 1; @@ -29,7 +32,7 @@ export default async function(followee: IUser, follower: IUser) { } }).catch(e => { if (e.code === 11000 && isRemoteUser(follower) && isLocalUser(followee)) { - console.log(`Accept => Insert duplicated ignore. ${follower._id} => ${followee._id}`); + logger.info(`Accept => Insert duplicated ignore. ${follower._id} => ${followee._id}`); incremented = 0; } else { throw e; diff --git a/src/services/note/create.ts b/src/services/note/create.ts index a52985b2f4..fa1a239000 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -495,7 +495,6 @@ async function insertNote(user: IUser, data: Option, tags: string[], emojis: str return null; } - console.error(e); throw 'something happened'; } } diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index a7ee1311ce..96d1dd8e53 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -30,7 +30,6 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise return rej('already reacted'); } - console.error(e); return rej('something happened'); }