mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-29 00:46:41 +07:00
added wtf command
This commit is contained in:
parent
0839684fff
commit
b28c34878b
5 changed files with 99 additions and 0 deletions
23
src/service/db/postgres/entities/Log.ts
Normal file
23
src/service/db/postgres/entities/Log.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class Log {
|
||||
@PrimaryGeneratedColumn()
|
||||
id!: number;
|
||||
@Column()
|
||||
level!: string;
|
||||
@Column({ type: "text" })
|
||||
message!: string;
|
||||
@Column("simple-json", { default: {}, nullable: false })
|
||||
body!: Record<any, any>;
|
||||
@CreateDateColumn()
|
||||
createdAt!: Date;
|
||||
@UpdateDateColumn()
|
||||
updatedAt!: Date;
|
||||
}
|
|
@ -9,6 +9,7 @@ import { Event } from "./entities/Event";
|
|||
import { Post } from "./entities/Post";
|
||||
import { LoggerConfig } from "../../logger/types";
|
||||
import { Request } from "./entities/Request";
|
||||
import { Log } from "./entities/Log";
|
||||
|
||||
const entities = [path.join(__dirname, "./entities/*")];
|
||||
|
||||
|
@ -18,6 +19,7 @@ export class PostgresDB implements Storage {
|
|||
private likes!: Repository<Like>;
|
||||
private posts!: Repository<Post>;
|
||||
private requests!: Repository<Request>;
|
||||
private logs!: Repository<Log>;
|
||||
|
||||
constructor(
|
||||
private config: PostgresConfig,
|
||||
|
@ -41,6 +43,7 @@ export class PostgresDB implements Storage {
|
|||
this.likes = this.connection.getRepository(Like);
|
||||
this.posts = this.connection.getRepository(Post);
|
||||
this.requests = this.connection.getRepository(Request);
|
||||
this.logs = this.connection.getRepository(Log);
|
||||
|
||||
logger.info(`db connected to ${this.config.uri}`);
|
||||
};
|
||||
|
@ -151,6 +154,27 @@ export class PostgresDB implements Storage {
|
|||
return requests[0];
|
||||
};
|
||||
|
||||
insertLog = async (
|
||||
level: string,
|
||||
message: string,
|
||||
body: Record<any, any>
|
||||
) => {
|
||||
return this.logs.save({ message, level, body });
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns last request with shift
|
||||
*/
|
||||
popLogs = async (take: number = 20, skip: number = 0) => {
|
||||
const requests = await this.logs.find({
|
||||
order: { createdAt: "DESC" },
|
||||
take,
|
||||
skip,
|
||||
});
|
||||
|
||||
return requests.reverse();
|
||||
};
|
||||
|
||||
healthcheck = async () => {
|
||||
try {
|
||||
await this.connection.query("SELECT 1");
|
||||
|
|
21
src/service/db/postgres/loggerTransport.ts
Normal file
21
src/service/db/postgres/loggerTransport.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import Transport, { TransportStreamOptions } from "winston-transport";
|
||||
import { PostgresDB } from "./index";
|
||||
import safeJson from "safe-json-stringify";
|
||||
|
||||
export class PgTransport extends Transport {
|
||||
constructor(private db: PostgresDB, private props: TransportStreamOptions) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
log = async (info, callback) => {
|
||||
try {
|
||||
await this.db.insertLog(info.level, info.message, info);
|
||||
} catch (e) {
|
||||
await this.db.insertLog(info.level, info.message, {
|
||||
dump: safeJson(info),
|
||||
});
|
||||
}
|
||||
|
||||
callback();
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue