Skip to content

Mastering Prisma Logging with Dynamic Configuration

Updated: at 02:12 PM

Mastering Prisma Logging with Dynamic Configuration: A Complete Guide


While developing applications that interface with databases, logging is a vital aspect. Prisma, a well-known ORM, offers logging options to capture database events, errors, and queries. But how can we make this more dynamic and controlled? In this post, we’re dissecting a nifty function called getPrismaOptions() to flexibly manage logging configurations using environment variables. This function can return various logging configurations based on the setup in an .env file, giving us the power to toggle logging levels without altering code.

The getPrismaOptions Function Explained

Complete Function Code

Firstly, let’s look at the complete code for getPrismaOptions().

type LogOption = {
  emit: "event" | "stdout";
  level: "query" | "error" | "info" | "warn";

function getPrismaOptions(): { log: LogOption[] } {
  const queryLogging = process.env.PRISMA_EVENT_QUERY !== "false"; // default true
  const errorLogging = process.env.PRISMA_STDOUT_ERROR !== "false"; // default true
  const infoLogging = process.env.PRISMA_STDOUT_INFO === "true"; // default false
  const warnLogging = process.env.PRISMA_STDOUT_WARN === "true"; // default false

  const logOptions: LogOption[] = [];

  if (queryLogging) {
      emit: "event",
      level: "query",

  if (errorLogging) {
      emit: "stdout",
      level: "error",

  if (infoLogging) {
      emit: "stdout",
      level: "info",

  if (warnLogging) {
      emit: "stdout",
      level: "warn",

  return {
    log: logOptions,

Setting Variables From an .env File

The key to the dynamic nature of this function is its reliance on environment variables, specifically set in an .env file.

Example of .env file content:


Here, you define the values for PRISMA_EVENT_QUERY, PRISMA_STDOUT_ERROR, PRISMA_STDOUT_INFO, and PRISMA_STDOUT_WARN. To use these in your Node.js application, you would require the dotenv package to load them.

Run npm install dotenv or yarn add dotenv, and then at the start of your application (typically in index.js or app.js), add:


This would load all the variables from .env into process.env, making them available in getPrismaOptions().

How Environment Variables Control Logging

The environment variables control whether certain types of logs are active:

For example, setting PRISMA_EVENT_QUERY to true in the .env file will enable query logging, while setting it to false will disable it.

Why You Need getPrismaOptions

Dynamic Configuration

One main advantage of this approach is that you can change your logging preferences dynamically, without touching the source code.

Environment-Specific Logging

With .env files, you can specify different configurations for development, staging, and production. For example, in development, you might need all logging levels, while in production, you may just require error logging for optimal performance.

Code Cleanliness

Consolidating your log configuration logic in one function makes your codebase cleaner and more maintainable.


By embracing getPrismaOptions(), you can efficiently manage Prisma’s logging capabilities, making your application not only more maintainable but also adaptable to various environments. Utilizing environment variables through an .env file gives you fine-grained control over what gets logged and where, offering a flexible development experience tailored to your needs.

Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects