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