Better logging

This commit is contained in:
syuilo 2019-02-03 16:45:13 +09:00
parent ca91709801
commit 75558add17
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
11 changed files with 62 additions and 68 deletions

View file

@ -24,6 +24,8 @@ import fetchMeta from '../../misc/fetch-meta';
import { GenerateVideoThumbnail } from './generate-video-thumbnail';
import { driveLogger } from './logger';
const logger = driveLogger.createSubLogger('register', 'yellow');
/***
* Save file
* @param path Path for original
@ -40,7 +42,7 @@ async function save(path: string, name: string, type: string, hash: string, size
let webpublicType = 'image/jpeg';
if (!metadata.uri) { // from local instance
driveLogger.info(`creating web image of ${name}`);
logger.info(`creating web image of ${name}`);
if (['image/jpeg'].includes(type)) {
webpublic = await sharp(path)
@ -81,10 +83,10 @@ async function save(path: string, name: string, type: string, hash: string, size
webpublicExt = 'png';
webpublicType = 'image/png';
} else {
driveLogger.info(`web image not created (not an image)`);
logger.info(`web image not created (not an image)`);
}
} else {
driveLogger.info(`web image not created (from remote)`);
logger.info(`web image not created (from remote)`);
}
// #endregion webpublic
@ -121,7 +123,7 @@ async function save(path: string, name: string, type: string, hash: string, size
try {
thumbnail = await GenerateVideoThumbnail(path);
} catch (e) {
driveLogger.error(`GenerateVideoThumbnail failed: ${e}`);
logger.error(`GenerateVideoThumbnail failed: ${e}`);
}
}
// #endregion thumbnail
@ -139,18 +141,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}`;
driveLogger.info(`uploading original: ${key}`);
logger.info(`uploading original: ${key}`);
const uploads = [
upload(key, fs.createReadStream(path), type)
];
if (webpublic) {
driveLogger.info(`uploading webpublic: ${webpublicKey}`);
logger.info(`uploading webpublic: ${webpublicKey}`);
uploads.push(upload(webpublicKey, webpublic, webpublicType));
}
if (thumbnail) {
driveLogger.info(`uploading thumbnail: ${thumbnailKey}`);
logger.info(`uploading thumbnail: ${thumbnailKey}`);
uploads.push(upload(thumbnailKey, thumbnail, thumbnailType));
}
@ -200,7 +202,7 @@ async function save(path: string, name: string, type: string, hash: string, size
fs.createReadStream(path).pipe(writeStream);
});
driveLogger.info(`original stored to ${originalFile._id}`);
logger.info(`original stored to ${originalFile._id}`);
// #endregion store original
// #region store webpublic
@ -220,7 +222,7 @@ async function save(path: string, name: string, type: string, hash: string, size
writeStream.end(webpublic);
});
driveLogger.info(`web stored ${webFile._id}`);
logger.info(`web stored ${webFile._id}`);
}
// #endregion store webpublic
@ -240,7 +242,7 @@ async function save(path: string, name: string, type: string, hash: string, size
writeStream.end(thumbnail);
});
driveLogger.info(`thumbnail stored ${tuhmFile._id}`);
logger.info(`thumbnail stored ${tuhmFile._id}`);
}
return originalFile;
@ -349,7 +351,7 @@ export default async function(
const [hash, [mime, ext], size] = await Promise.all([calcHash, detectMime, getFileSize]);
driveLogger.info(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`);
logger.info(`hash: ${hash}, mime: ${mime}, ext: ${ext}, size: ${size}`);
// detect name
const detectedName = name || (ext ? `untitled.${ext}` : 'untitled');
@ -363,7 +365,7 @@ export default async function(
});
if (much) {
driveLogger.info(`file with same hash is found: ${much._id}`);
logger.info(`file with same hash is found: ${much._id}`);
return much;
}
}
@ -393,7 +395,7 @@ export default async function(
return 0;
});
driveLogger.info(`drive usage is ${usage}`);
logger.info(`drive usage is ${usage}`);
const instance = await fetchMeta();
const driveCapacity = 1024 * 1024 * (isLocalUser(user) ? instance.localDriveCapacityMb : instance.remoteDriveCapacityMb);
@ -436,12 +438,12 @@ export default async function(
// Calc width and height
const calcWh = async () => {
driveLogger.info('calculating image width and height...');
logger.debug('calculating image width and height...');
// Calculate width and height
const meta = await img.metadata();
driveLogger.info(`image width and height is calculated: ${meta.width}, ${meta.height}`);
logger.debug(`image width and height is calculated: ${meta.width}, ${meta.height}`);
properties['width'] = meta.width;
properties['height'] = meta.height;
@ -449,7 +451,7 @@ export default async function(
// Calc average color
const calcAvg = async () => {
driveLogger.info('calculating average color...');
logger.debug('calculating average color...');
try {
const info = await (img as any).stats();
@ -458,7 +460,7 @@ export default async function(
const g = Math.round(info.channels[1].mean);
const b = Math.round(info.channels[2].mean);
driveLogger.info(`average color is calculated: ${r}, ${g}, ${b}`);
logger.debug(`average color is calculated: ${r}, ${g}, ${b}`);
const value = info.isOpaque ? [r, g, b] : [r, g, b, 255];
@ -514,14 +516,14 @@ export default async function(
} catch (e) {
// duplicate key error (when already registered)
if (e.code === 11000) {
driveLogger.info(`already registered ${metadata.uri}`);
logger.info(`already registered ${metadata.uri}`);
driveFile = await DriveFile.findOne({
'metadata.uri': metadata.uri,
'metadata.userId': user._id
});
} else {
driveLogger.error(e);
logger.error(e);
throw e;
}
}
@ -529,7 +531,7 @@ export default async function(
driveFile = await (save(path, detectedName, mime, hash, size, metadata));
}
driveLogger.succ(`drive file has been created ${driveFile._id}`);
logger.succ(`drive file has been created ${driveFile._id}`);
pack(driveFile).then(packedFile => {
// Publish driveFileCreated event

View file

@ -1,7 +1,5 @@
import * as fs from 'fs';
import * as URL from 'url';
import * as debug from 'debug';
import * as tmp from 'tmp';
import * as request from 'request';
@ -10,8 +8,9 @@ import create from './add-file';
import config from '../../config';
import { IUser } from '../../models/user';
import * as mongodb from 'mongodb';
import { driveLogger } from './logger';
const log = debug('misskey:drive:upload-from-url');
const logger = driveLogger.createSubLogger('downloader');
export default async (
url: string,
@ -22,14 +21,14 @@ export default async (
force = false,
link = false
): Promise<IDriveFile> => {
log(`REQUESTED: ${url}`);
logger.info(`REQUESTED: ${url}`);
let name = URL.parse(url).pathname.split('/').pop();
if (!validateFileName(name)) {
name = null;
}
log(`name: ${name}`);
logger.info(`name: ${name}`);
// Create temp file
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
@ -82,10 +81,10 @@ export default async (
try {
driveFile = await create(user, path, name, null, folderId, force, link, url, uri, sensitive);
log(`got: ${driveFile._id}`);
logger.succ(`got: ${driveFile._id}`);
} catch (e) {
error = e;
log(`failed: ${e}`);
logger.error(`failed: ${e}`);
}
// clean-up