From 238c6a428be14f484dac102289af183aec2a48fd Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Feb 2019 04:18:27 +0900 Subject: [PATCH] [Server] Use logger for logging --- src/remote/activitypub/logger.ts | 3 ++ src/remote/activitypub/request.ts | 8 +++--- src/remote/activitypub/resolver.ts | 6 ++-- src/services/drive/add-file.ts | 44 ++++++++++++++---------------- src/services/drive/logger.ts | 3 ++ 5 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 src/remote/activitypub/logger.ts create mode 100644 src/services/drive/logger.ts diff --git a/src/remote/activitypub/logger.ts b/src/remote/activitypub/logger.ts new file mode 100644 index 000000000..a611f940c --- /dev/null +++ b/src/remote/activitypub/logger.ts @@ -0,0 +1,3 @@ +import { remoteLogger } from "../logger"; + +export const apLogger = remoteLogger.createSubLogger('ap', 'magenta'); diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index 366d7e19a..519c8026b 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -1,7 +1,6 @@ import { request } from 'https'; import { sign } from 'http-signature'; import { URL } from 'url'; -import * as debug from 'debug'; import * as crypto from 'crypto'; import { lookup, IRunOptions } from 'lookup-dns-cache'; import * as promiseAny from 'promise-any'; @@ -9,11 +8,12 @@ import * as promiseAny from 'promise-any'; import config from '../../config'; import { ILocalUser } from '../../models/user'; import { publishApLogStream } from '../../stream'; +import { apLogger } from './logger'; -const log = debug('misskey:activitypub:deliver'); +export const logger = apLogger.createSubLogger('deliver'); export default (user: ILocalUser, url: string, object: any) => new Promise(async (resolve, reject) => { - log(`--> ${url}`); + logger.info(`--> ${url}`); const timeout = 10 * 1000; @@ -43,7 +43,7 @@ export default (user: ILocalUser, url: string, object: any) => new Promise(async 'Digest': `SHA-256=${hash}` } }, res => { - log(`${url} --> ${res.statusCode}`); + logger.info(`${url} --> ${res.statusCode}`); if (res.statusCode >= 400) { reject(res); diff --git a/src/remote/activitypub/resolver.ts b/src/remote/activitypub/resolver.ts index 8ec0d125a..bff84f578 100644 --- a/src/remote/activitypub/resolver.ts +++ b/src/remote/activitypub/resolver.ts @@ -1,9 +1,9 @@ import * as request from 'request-promise-native'; -import * as debug from 'debug'; import { IObject } from './type'; import config from '../../config'; +import { apLogger } from './logger'; -const log = debug('misskey:activitypub:resolver'); +export const logger = apLogger.createSubLogger('resolver'); export default class Resolver { private history: Set; @@ -67,7 +67,7 @@ export default class Resolver { !object['@context'].includes('https://www.w3.org/ns/activitystreams') : object['@context'] !== 'https://www.w3.org/ns/activitystreams' )) { - log(`invalid response: ${value}`); + logger.error(`invalid response: ${value}`); throw new Error('invalid response'); } diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index c6fa44a3e..a7797351a 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -3,7 +3,6 @@ import * as fs from 'fs'; import * as mongodb from 'mongodb'; import * as crypto from 'crypto'; -import * as debug from 'debug'; import * as Minio from 'minio'; import * as uuid from 'uuid'; import * as sharp from 'sharp'; @@ -23,8 +22,7 @@ import driveChart from '../../chart/drive'; import perUserDriveChart from '../../chart/per-user-drive'; import fetchMeta from '../../misc/fetch-meta'; import { GenerateVideoThumbnail } from './generate-video-thumbnail'; - -const log = debug('misskey:drive:add-file'); +import { driveLogger } from './logger'; /*** * Save file @@ -42,7 +40,7 @@ async function save(path: string, name: string, type: string, hash: string, size let webpublicType = 'image/jpeg'; if (!metadata.uri) { // from local instance - log(`creating web image`); + driveLogger.info(`creating web image of ${name}`); if (['image/jpeg'].includes(type)) { webpublic = await sharp(path) @@ -83,10 +81,10 @@ async function save(path: string, name: string, type: string, hash: string, size webpublicExt = 'png'; webpublicType = 'image/png'; } else { - log(`web image not created (not an image)`); + driveLogger.info(`web image not created (not an image)`); } } else { - log(`web image not created (from remote)`); + driveLogger.info(`web image not created (from remote)`); } // #endregion webpublic @@ -123,7 +121,7 @@ async function save(path: string, name: string, type: string, hash: string, size try { thumbnail = await GenerateVideoThumbnail(path); } catch (e) { - console.log(`GenerateVideoThumbnail failed: ${e}`); + driveLogger.error(`GenerateVideoThumbnail failed: ${e}`); } } // #endregion thumbnail @@ -141,18 +139,18 @@ async function save(path: string, name: string, type: string, hash: string, size const webpublicKey = `${config.drive.prefix}/${uuid.v4()}.${webpublicExt}`; const thumbnailKey = `${config.drive.prefix}/${uuid.v4()}.${thumbnailExt}`; - log(`uploading original: ${key}`); + driveLogger.info(`uploading original: ${key}`); const uploads = [ upload(key, fs.createReadStream(path), type) ]; if (webpublic) { - log(`uploading webpublic: ${webpublicKey}`); + driveLogger.info(`uploading webpublic: ${webpublicKey}`); uploads.push(upload(webpublicKey, webpublic, webpublicType)); } if (thumbnail) { - log(`uploading thumbnail: ${thumbnailKey}`); + driveLogger.info(`uploading thumbnail: ${thumbnailKey}`); uploads.push(upload(thumbnailKey, thumbnail, thumbnailType)); } @@ -202,7 +200,7 @@ async function save(path: string, name: string, type: string, hash: string, size fs.createReadStream(path).pipe(writeStream); }); - log(`original stored to ${originalFile._id}`); + driveLogger.info(`original stored to ${originalFile._id}`); // #endregion store original // #region store webpublic @@ -222,7 +220,7 @@ async function save(path: string, name: string, type: string, hash: string, size writeStream.end(webpublic); }); - log(`web stored ${webFile._id}`); + driveLogger.info(`web stored ${webFile._id}`); } // #endregion store webpublic @@ -242,7 +240,7 @@ async function save(path: string, name: string, type: string, hash: string, size writeStream.end(thumbnail); }); - log(`thumbnail stored ${tuhmFile._id}`); + driveLogger.info(`thumbnail stored ${tuhmFile._id}`); } return originalFile; @@ -351,7 +349,7 @@ export default async function( const [hash, [mime, ext], size] = await Promise.all([calcHash, detectMime, getFileSize]); - log(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`); + driveLogger.info(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`); // detect name const detectedName = name || (ext ? `untitled.${ext}` : 'untitled'); @@ -365,7 +363,7 @@ export default async function( }); if (much) { - log(`file with same hash is found: ${much._id}`); + driveLogger.info(`file with same hash is found: ${much._id}`); return much; } } @@ -395,7 +393,7 @@ export default async function( return 0; }); - log(`drive usage is ${usage}`); + driveLogger.info(`drive usage is ${usage}`); const instance = await fetchMeta(); const driveCapacity = 1024 * 1024 * (isLocalUser(user) ? instance.localDriveCapacityMb : instance.remoteDriveCapacityMb); @@ -438,12 +436,12 @@ export default async function( // Calc width and height const calcWh = async () => { - log('calculate image width and height...'); + driveLogger.info('calculating image width and height...'); // Calculate width and height const meta = await img.metadata(); - log(`image width and height is calculated: ${meta.width}, ${meta.height}`); + driveLogger.info(`image width and height is calculated: ${meta.width}, ${meta.height}`); properties['width'] = meta.width; properties['height'] = meta.height; @@ -451,7 +449,7 @@ export default async function( // Calc average color const calcAvg = async () => { - log('calculate average color...'); + driveLogger.info('calculating average color...'); try { const info = await (img as any).stats(); @@ -460,7 +458,7 @@ export default async function( const g = Math.round(info.channels[1].mean); const b = Math.round(info.channels[2].mean); - log(`average color is calculated: ${r}, ${g}, ${b}`); + driveLogger.info(`average color is calculated: ${r}, ${g}, ${b}`); const value = info.isOpaque ? [r, g, b] : [r, g, b, 255]; @@ -516,14 +514,14 @@ export default async function( } catch (e) { // duplicate key error (when already registered) if (e.code === 11000) { - log(`already registered ${metadata.uri}`); + driveLogger.info(`already registered ${metadata.uri}`); driveFile = await DriveFile.findOne({ 'metadata.uri': metadata.uri, 'metadata.userId': user._id }); } else { - console.error(e); + driveLogger.error(e); throw e; } } @@ -531,7 +529,7 @@ export default async function( driveFile = await (save(path, detectedName, mime, hash, size, metadata)); } - log(`drive file has been created ${driveFile._id}`); + driveLogger.succ(`drive file has been created ${driveFile._id}`); pack(driveFile).then(packedFile => { // Publish driveFileCreated event diff --git a/src/services/drive/logger.ts b/src/services/drive/logger.ts new file mode 100644 index 000000000..979d282cc --- /dev/null +++ b/src/services/drive/logger.ts @@ -0,0 +1,3 @@ +import Logger from "../../misc/logger"; + +export const driveLogger = new Logger('drive', 'blue');