Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access logger in client or server from plugin #26967

Open
briandee opened this issue Apr 26, 2024 · 1 comment
Open

Unable to access logger in client or server from plugin #26967

briandee opened this issue Apr 26, 2024 · 1 comment

Comments

@briandee
Copy link

briandee commented Apr 26, 2024

Environment

Environment
Operating System: Mac OS Ventura
Node Version: v20.10.0
Nuxt Version: 3.10.0
Package Manager: npm@10.2.3

Reproduction

https://stackblitz.com/edit/github-bfnrsx-dxkhza?file=src%2Fplugins%2Flogger.client.ts,src%2Fplugins%2Flogger.server.ts,src%2Fmodule.ts,package.json,src%2FlogLevels.ts,playground%2Fnuxt.config.ts

Here is the configuration to access the module

      export default defineNuxtConfig({
        nitro: {
          serveStatic: true,
        },
      
        css: ["@/assets/css/global.css"],
      
        modules: ["@vueuse/nuxt", "cd-logger"],
      
        runtimeConfig: {
          public: {
            firstModule: {
              loggerOptions: {
                appName: "c_logger", 
                useIsoTime: false,  
                outPutOnSingleLine: true,  
                clientOptions: {
                  level: "info",
                }, 
                serverOptions: {
                  level: "info",    
                  useFormattersAndSerializers: true,
                },
              },
            },
          },
        },
      });

I attempt to access it as follows

        import axios from "axios";
        import { defineEventHandler, readBody } from "h3";

        const LOGIN_URL =
        process.env.LOGIN_URL ||
        "http://app-dev.com/api/v1/server/secure/users/account/login";

        export default defineEventHandler(async (event) => {
        try {
            try {
            console.log("Context:: - ", event);
            console.log("LOGGER:: - ", this.$logger);
            } catch (error) {
            console.error("Unexpected error thrown", error);
            }
            const loginRequest = await readBody(event);
            const response = await axios.post(LOGIN_URL, loginRequest);
            return { json: response.data, status: response.status };
        } catch (error) {
            console.error(error);
        }
        });


Describe the bug

Attempting to access the logger in server/api returns undefined but using const {$logger} = useNuxtApp() in client works. I can go to localhost:3000 after running npm run dev and load it without errors. The plugin provided used the information in the documentation so what am I missing? I thought I could simply be able to access the logger in the client using this.$logger without useNuxtApp but that does not work. I am unable to access the logger in the event handler which always returns undefined

Additional context

Unfortunately, I cannot provide the published plugin because it is in a private npm repo. I can access it with npm i cd-logger --save-prod and include it as a module without errors so it is difficult to understand why it is undefined when there are no errors. When I run 'npm run prepack' the node_modules is @nuxt/kit is included, is included in the published module. Is that expected?

Can I use addServerPlugin, and if so how do I reference the logger in the plugin?

Logs

No response

Copy link

stackblitz bot commented Apr 26, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant