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_SIZE=""
# PL_LOGGING_MONGODB_MAX_DOCUMENTS="" # 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 # MIXPANEL
# #

View File

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

View File

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