Skip to content

Commit

Permalink
Add Cloud Logging Support
Browse files Browse the repository at this point in the history
  • Loading branch information
caipira113 committed Jun 17, 2023
1 parent c5ddbb4 commit 0a883ef
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .config/docker_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ id: 'aid'
# IP address family used for outgoing request (ipv4, ipv6 or dual)
#outgoingAddressFamily: ipv4

# Cloud Logging
#cloudLogging:
# projectId: example-project-id
# saKeyPath: /path/to/service-account-key.json
# logName: misskey

# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

Expand Down
6 changes: 6 additions & 0 deletions .config/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ id: 'aid'
# IP address family used for outgoing request (ipv4, ipv6 or dual)
#outgoingAddressFamily: ipv4

# Cloud Logging
#cloudLogging:
# projectId: example-project-id
# saKeyPath: /path/to/service-account-key.json
# logName: misskey

# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

Expand Down
6 changes: 6 additions & 0 deletions .devcontainer/devcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ id: 'aid'
# IP address family used for outgoing request (ipv4, ipv6 or dual)
#outgoingAddressFamily: ipv4

# Cloud Logging
#cloudLogging:
# projectId: example-project-id
# saKeyPath: /path/to/service-account-key.json
# logName: misskey

# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

Expand Down
6 changes: 6 additions & 0 deletions chart/files/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ id: "aid"
# IP address family used for outgoing request (ipv4, ipv6 or dual)
#outgoingAddressFamily: ipv4

# Cloud Logging
#cloudLogging:
# projectId: example-project-id
# saKeyPath: /path/to/service-account-key.json
# logName: misskey

# Proxy for HTTP/HTTPS
#proxy: http://127.0.0.1:3128

Expand Down
2 changes: 2 additions & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@fastify/multipart": "7.6.0",
"@fastify/static": "6.10.2",
"@fastify/view": "7.4.1",
"@google-cloud/logging": "^10.5.0",
"@google-cloud/translate": "^7.2.1",
"@nestjs/common": "9.4.2",
"@nestjs/core": "9.4.2",
Expand Down Expand Up @@ -146,6 +147,7 @@
"slacc": "0.0.9",
"strict-event-emitter-types": "2.0.0",
"stringz": "2.1.0",
"strip-ansi": "^7.1.0",
"summaly": "github:misskey-dev/summaly",
"systeminformation": "5.17.16",
"tinycolor2": "1.6.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export type Source = {
deliverJobMaxAttempts?: number;
inboxJobMaxAttempts?: number;

cloudLogging?: {
projectId: string;
saKeyPath: string;
logName?: string;
}

mediaProxy?: string;
proxyRemoteFiles?: boolean;
videoThumbnailGenerator?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/backend/src/core/LoggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import type { KEYWORD } from 'color-convert/conversions';

@Injectable()
export class LoggerService {
private cloudLogging;
constructor(
@Inject(DI.config)
private config: Config,
) {
if (this.config.cloudLogging) {
this.cloudLogging = this.config.cloudLogging;
}
}

@bindThis
public getLogger(domain: string, color?: KEYWORD | undefined, store?: boolean) {
return new Logger(domain, color, store);
return new Logger(domain, color, store, this.cloudLogging);
}
}
49 changes: 46 additions & 3 deletions packages/backend/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import cluster from 'node:cluster';
import util from 'util';
import chalk from 'chalk';
import { default as convertColor } from 'color-convert';
import { format as dateFormat } from 'date-fns';
import { Logging } from '@google-cloud/logging';
import stripAnsi from 'strip-ansi';
import { bindThis } from '@/decorators.js';
import { envOption } from './env.js';
import type { KEYWORD } from 'color-convert/conversions';
Expand All @@ -12,18 +15,21 @@ type Context = {
};

type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
type CloudLogging = any | undefined;

export default class Logger {
private context: Context;
private parentLogger: Logger | null = null;
private store: boolean;
private clConfig?: CloudLogging;

constructor(context: string, color?: KEYWORD, store = true) {
constructor(context: string, color?: KEYWORD, store = true, clConfig?: CloudLogging) {
this.context = {
name: context,
color: color,
};
this.store = store;
this.clConfig = clConfig;
}

@bindThis
Expand All @@ -44,7 +50,8 @@ export default class Logger {
return;
}

const time = dateFormat(new Date(), 'HH:mm:ss');
const timestamp = new Date();
const time = dateFormat(timestamp, 'HH:mm:ss');
const worker = cluster.isPrimary ? '*' : cluster.worker!.id;
const l =
level === 'error' ? important ? chalk.bgRed.white('ERR ') : chalk.red('ERR ') :
Expand All @@ -66,7 +73,43 @@ export default class Logger {
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;

console.log(important ? chalk.bold(log) : log);
if (level === 'error' && data) console.log(data);
if (level === 'error' && data) {
console.log(data);
this.writeCloudLogging(level, log, timestamp, data);
} else {
this.writeCloudLogging(level, log, timestamp, null);
}
}

private async writeCloudLogging(level: Level, message: string, time: Date, data?: Record<string, any> | null) {
if (!this.clConfig) return;
if (!this.clConfig.projectId || !this.clConfig.saKeyPath) return;

let lv = level;
if (level === 'success') lv = 'info';

const projectId = this.clConfig.projectId;
const logging = new Logging({ projectId: projectId, keyFilename: this.clConfig.saKeyPath });
const logName = this.clConfig.logName ?? 'misskey';
const log = logging.log(logName);
const logMessage = stripAnsi(message);

const metadata = {
severity: lv.toUpperCase(),
resource: {
type: 'global',
timestamp: time,
},
labels: {
name: `${this.context.name}`,
color: `${this.context.color}`,
},
};

const dataString = data ? '\n' + util.inspect(data, { depth: null }) : '';
const entry = log.entry(metadata, logMessage + dataString);

await log.write(entry);
}

@bindThis
Expand Down
104 changes: 102 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a883ef

Please sign in to comment.