diff --git a/docs/examples/config/example.env b/docs/examples/config/example.env index c764173a..e589d1c2 100644 --- a/docs/examples/config/example.env +++ b/docs/examples/config/example.env @@ -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 # diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts index b5bc633f..e135807e 100644 --- a/packages/server/src/config.ts +++ b/packages/server/src/config.ts @@ -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; } diff --git a/packages/server/src/init.ts b/packages/server/src/init.ts index bc9a64e8..0de4de7c 100644 --- a/packages/server/src/init.ts +++ b/packages/server/src/init.ts @@ -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(); diff --git a/packages/server/src/logging/postgres.ts b/packages/server/src/logging/postgres.ts new file mode 100644 index 00000000..62e51264 --- /dev/null +++ b/packages/server/src/logging/postgres.ts @@ -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; + } +}