#Configure logs to `/dev/stdout` for next13 application in docker container

19 messages · Page 1 of 1 (latest)

ornate finch
#

How can I configure to see logs using docker logs ? By default I see only "Listening on port 3000 url: ..."

sinful aurora
#

try add console.log() inside middleware, getServerSideProps() func or api. i run Next.js as one of the k8s micro services on GCP. it prints with console.log. no config required.

ornate finch
ornate finch
#

@sinful aurora question how to get console.logs from pages using middleware ? 🤔

sinful aurora
#

@ornate finch sorry if i get you confused. actually console.log() in pages print STDOUT as well as middleware.
but one caveat. Next.js prerender pages when you run docker build. and for pages that have getServerSideProps(), ie. SSR, in case of client side navigation with Link, the browser calls the func like RPC and get JSON data to render on client side. so console.log won't print.
see this for more details https://www.joshwcomeau.com/nextjs/refreshing-server-side-props/#why-it-works-2

Next allows you to do server-side data-fetching, but what happens when that data needs to change on the client? This brief tutorial shows how to re-fetch the props without doing a full server reload.

#

i just tested locally on my mac's Docker. both console.log print to the console.

ornate finch
#

@sinful aurora are you usine app route ? I am facing type issue every time:
"getServerSideProps" is not a valid Page export field.

sinful aurora
#

it’s Page Router, not App Router

#

first off, just run stand alone without docker so we can identify the problem. it sounds you have another issue instead of Docker.

ornate finch
#

@sinful aurora I can not use getServerSideProps in App router . I am still not able to see any logs using app router

sinful aurora
#

@ornate finch oh. yes indeed, you can NOT use getServerSidePros in App Router because it is for Page Router.
As for Server Components of App Router, pages can be pre-rendered as build time. in that case, you want to force invalidate page cache like this:

import {prisma} from '@/lib/prisma';

export const revalidate = 60 // revalidate this segment every 60 seconds

export default async function Posts() {
    const rows = await prisma.user.findMany();
    console.log(rows)

    return (
        <div>
            {rows.map((row) => (
                <div key={row.id}>
                    {JSON.stringify(row)}
                </div>
            ))}
        </div>
    );
}

otherwise the page won't be rendered, thus console.log won't be executed as such.

ornate finch
#

ha works! 😄

sinful aurora
#

cool. notice that pages are pre-rendered when you run Docker build. this is a bit problem when running on k8s to set ENV variables.

ornate finch
#

yes I notice this and still trying to figure out

sinful aurora
#

i answered ENV related issues before here. so you can find it by searching.

ornate finch
#

I had to do this:

sinful aurora
#

wow cool!

pulsar isle
#

Did you manage to get logging working? Having issues with my next 13 app in docker not showing any logs for api calls. I’ve tried adding revalidate to the route but no luck.