Implement postgres logger

This commit is contained in:
Martin Kleinschrodt 2022-07-29 17:29:07 +02:00
parent a7c58f693d
commit cc092fae03
4 changed files with 47 additions and 5 deletions

View File

@ -184,6 +184,22 @@
# PL_LOGGING_MONGODB_MAX_SIZE=""
# PL_LOGGING_MONGODB_MAX_DOCUMENTS=""
# -----------------------------------------------------------------------------
# POSTGRES
#
# Use postgresql as log storage: https://www.npmjs.com/package/pg
# -----------------------------------------------------------------------------
# PL_LOGGING_BACKEND=postgres
# PL_LOGGING_POSTGRES_HOST=localhost
# PL_LOGGING_POSTGRES_PORT=5432
# PL_LOGGING_POSTGRES_DATABASE=padloc
# PL_LOGGING_POSTGRES_USER=padloc
# PL_LOGGING_POSTGRES_PASSWORD=""
# PL_LOGGING_POSTGRES_TLS=false
# PL_LOGGING_POSTGRES_TLS_CAFILE=""
# PL_LOGGING_POSTGRES_TLS_REJECT_UNAUTHORIZED=true
# -----------------------------------------------------------------------------
# MIXPANEL
#

View File

@ -82,7 +82,7 @@ export class LoggingConfig extends Config {
}
@ConfigParam()
backend: "void" | "mongodb" | "mixpanel" = "void";
backend: "void" | "mongodb" | "postgres" | "mixpanel" = "void";
@ConfigParam()
secondaryBackend?: "mongodb" | "mixpanel";
@ -90,6 +90,9 @@ export class LoggingConfig extends Config {
@ConfigParam(MongoDBStorageConfig)
mongodb?: MongoDBStorageConfig;
@ConfigParam(PostgresConfig)
postgres?: PostgresConfig;
@ConfigParam(MixpanelConfig)
mixpanel?: MixpanelConfig;
}

View File

@ -37,6 +37,7 @@ import { stripPropertiesRecursive } from "@padloc/core/src/util";
import { DirectoryProvisioner } from "./provisioning/directory";
import { ScimServer, ScimServerConfig } from "./scim";
import { DirectoryProvider, DirectorySync } from "@padloc/core/src/directory";
import { PostgresLogger } from "./logging/postgres";
const rootDir = resolve(__dirname, "../../..");
const assetsDir = resolve(rootDir, process.env.PL_ASSETS_DIR || "assets");
@ -74,7 +75,7 @@ async function initDataStorage(config: DataStorageConfig) {
}
}
async function initLogger({ backend, secondaryBackend, mongodb, mixpanel }: LoggingConfig) {
async function initLogger({ backend, secondaryBackend, mongodb, postgres, mixpanel }: LoggingConfig) {
let primaryLogger: Logger;
switch (backend) {
@ -82,9 +83,15 @@ async function initLogger({ backend, secondaryBackend, mongodb, mixpanel }: Logg
if (!mongodb) {
throw "PL_LOGGING_BACKEND was set to 'mongodb', but no related configuration was found!";
}
const storage = new MongoDBStorage(mongodb);
await storage.init();
primaryLogger = new MongoDBLogger(storage);
const mongoStorage = new MongoDBStorage(mongodb);
await mongoStorage.init();
primaryLogger = new MongoDBLogger(mongoStorage);
break;
case "postgres":
if (!postgres) {
throw "PL_LOGGING_BACKEND was set to 'postgres', but no related configuration was found!";
}
primaryLogger = new PostgresLogger(new PostgresStorage(postgres));
break;
case "void":
primaryLogger = new VoidLogger();

View File

@ -0,0 +1,16 @@
import { Logger, LogEvent } from "@padloc/core/src/logging";
import { PostgresStorage } from "../storage/postgres";
export class PostgresLogger implements Logger {
constructor(private _storage: PostgresStorage) {}
log(type: string, data?: any) {
const event = new LogEvent(type, data);
(async () => {
try {
this._storage.save(event);
} catch (e) {}
})();
return event;
}
}