FeathersJS+Sequelize-MariaDB项目替换自定义logger后列表查询报'logger is not a function'500错误排查求助
Let's break down why you're seeing this 500 error specifically on find (GET list) requests, even though your custom logger works elsewhere:
Root Cause Hypotheses
The error happens because some component (either Feathers internal code or Sequelize) is trying to call your logger as a direct function (logger('message')), but your custom logger is structured as an object with methods (like logger.info() or logger.error()) — or vice versa. Here's the most likely scenarios:
Sequelize's Implicit Logger Fallback
When you initialize Sequelize without explicitly setting aloggeroption, it may default to using your Feathers app'sapp.logger. Sequelize expects the logger to be a callable function (likeconsole.log), but if your custom logger is an object with methods (e.g., a Winston/Pino instance), calling it directly will throw the "not a function" error. This triggers onfindrequests because that's when Sequelize runs database queries and tries to log them.Feathers Internal Error Handling Logic
When yourbefore-findhook throws a 401 unauthorized error, Feathers' default error handler tries to log the error. If your custom logger doesn't support the way Feathers calls it (e.g., Feathers usesapp.logger.error(err)but your logger is a raw function, or Feathers triesapp.logger(err)but your logger is an object), it will fail.Incomplete Logger Compatibility
Your logger works in your own code because you're calling it the right way, but third-party modules (Feathers/Sequelize) have different expectations for logger interfaces.
Step-by-Step Fixes
1. Make Your Logger Compatible with Both Call Styles
Ensure your custom logger works both as a direct function and as an object with methods. For example:
// ../src/utils/logger.js // Base logger function const log = (message, level = 'info') => { // Your custom logging logic here (e.g., write to file, send to monitoring) console.log(`[${level.toUpperCase()}] ${message}`); }; // Add methods for Feathers-style calls log.info = (message) => log(message, 'info'); log.error = (message) => log(message, 'error'); log.debug = (message) => log(message, 'debug'); // Allow direct function calls (for Sequelize-style usage) module.exports = log;
2. Explicitly Configure Sequelize's Logger
In your Sequelize initialization file (usually src/models/sequelize.js), explicitly set the logger to your custom logger or disable it entirely to avoid conflicts:
const { Sequelize } = require('sequelize'); const logger = require('../utils/logger'); const sequelize = new Sequelize(process.env.DATABASE_URL, { // Other config options logger: logger, // Use your compatible logger // OR disable Sequelize logging entirely // logging: false });
3. Verify Feathers Error Handler Compatibility
Feathers' default error handler (@feathersjs/errors/dist/error-handler) calls app.logger.error(err) for errors. Double-check that your logger has an error method (like we added in step 1). If you've customized the error handler, ensure it's using your logger correctly.
4. Debug the Exact Call Site
To pinpoint which component is triggering the error, add a quick wrapper around your logger to log where it's being called from:
// ../src/utils/logger.js const originalLog = (message, level = 'info') => { // Your original logic }; // Wrapper to track call sites const log = (...args) => { console.log('Logger called from:', new Error().stack.split('\n')[2]); return originalLog(...args); }; // Add methods as before log.info = (...args) => log(...args, 'info'); log.error = (...args) => log(...args, 'error'); module.exports = log;
This will print the stack trace line where the logger is being invoked, letting you see if it's coming from Sequelize, Feathers' error handler, or another source.
Final Notes
Since your create request works, the issue is definitely tied to the find flow — either the Sequelize query logging or the error handling when your auth check fails. The fixes above should cover both scenarios.
内容的提问来源于stack exchange,提问作者kimmik

