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

Use loglevel for recording logs #18425

Closed
Palid opened this issue Aug 6, 2021 · 2 comments
Closed

Use loglevel for recording logs #18425

Palid opened this issue Aug 6, 2021 · 2 comments

Comments

@Palid
Copy link
Contributor

Palid commented Aug 6, 2021

Replace all console.log with https://github.com/pimterry/loglevel .debug calls for readability and to make a first step towards better structured logging, so that we'll only see proper development informations in the console instead of an enormous flood of everything, yet rageshake will still work properly.

Use below jscodeshift transform script:

const j = require('jscodeshift');
const addImports = require('jscodeshift-add-imports');

const { statement } = j.template;

const consoleMethods = [
    // 'error',
    // 'log',
    // 'warn',
    'info',
];

module.exports = function(fileInfo, api) {
    const root = api.jscodeshift(fileInfo.source);
    let foundConsoleCall = false;
    const newSource = root
        .find(j.MemberExpression, (node) => {
            const { object, property } = node;
            const isConsoleLog = (
                object.type === 'Identifier' &&
                object.name === 'console' &&
                property.type === 'Identifier' &&
                consoleMethods.includes(property.name)
            );

            return isConsoleLog;
        })
        .forEach(function(path) {
            const { value } = path;
            if (value.object.name === 'console' && consoleMethods.includes(value.property.name)) {
                foundConsoleCall = true;
                value.object.name = 'logger';
            }
        });

    if (foundConsoleCall) {
        addImports(root, [
            statement`import { logger } from "matrix-js-sdk/src/logger";`,
        ]);
    }

    return newSource.toSource();
};

To run the command:

  1. yarn add -D jscodeshift jscodeshift-add-imports (npx doesn't work with those packages due to wrong paths)
  2. Save above script as transform.js in the root directory of the package's src you want to update
  3. Run following command: node node_modules/.bin/jscodeshift --extensions=tsx,js,jsx --parser=tsx -t ./transform.js ./src/ && node node_modules/.bin/jscodeshift --extensions=ts --parser=ts -t ./transform.js ./src/
  4. There's no fourth point, you should be done!

Optimizing the flow isn't really useful, as tsx parser has some issues with .ts files. It takes a few seconds anyways.
If you want to change the console.info into logger.info just change CONSOLE_METHOD_NAME to something else, I don't think it can get much simpler. 😄

@Palid Palid added T-Enhancement A-DevTools /devtools, show hidden events, etc. A-Developer-Experience labels Aug 6, 2021
@Palid Palid added this to In discussion in Developer experience via automation Aug 6, 2021
@jryans
Copy link
Collaborator

jryans commented Aug 6, 2021

Please expose the logger instance to the console somehow so that e.g. I can turn them all back on via setLevel without changing code when desirable.

@Palid Palid moved this from In discussion to Ready to develop in Developer experience Aug 18, 2021
@Palid Palid added this to Next Sprint in Web App Team Sep 8, 2021
@novocaine novocaine moved this from Next Sprint to Current Sprint (RC 21/9) in Web App Team Sep 8, 2021
@novocaine novocaine added this to the App Team 1.8.4 milestone Sep 8, 2021
@novocaine novocaine changed the title Structurize app logs (replace console.log) Use loglevel for recording logs Sep 9, 2021
@Palid Palid moved this from Current Sprint (RC 21/9) to In Progress in Web App Team Sep 17, 2021
Palid added a commit to matrix-org/matrix-react-sdk that referenced this issue Sep 21, 2021
This commit replaces all the `console.log` to `logger.log` via an automated script.
Related: element-hq/element-web#18425
@novocaine novocaine modified the milestones: App Team 1.8.6, App Team 1.8.7 Sep 22, 2021
@Palid Palid moved this from In Progress to Current Sprint (RC 19/10) in Web App Team Oct 5, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 15, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 15, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 15, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 15, 2021
Palid pushed a commit that referenced this issue Oct 15, 2021
Palid pushed a commit that referenced this issue Oct 15, 2021
Palid pushed a commit that referenced this issue Oct 15, 2021
@Palid Palid moved this from Current Sprint (RC 19/10) to In Review in Web App Team Oct 15, 2021
@Palid
Copy link
Contributor Author

Palid commented Oct 15, 2021

Waiting for review:
matrix-react-sdk: https://github.com/matrix-org/matrix-react-sdk/pull/
element-web: #19410

Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 18, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 18, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 18, 2021
Palid pushed a commit to matrix-org/matrix-react-sdk that referenced this issue Oct 18, 2021
Palid pushed a commit that referenced this issue Oct 18, 2021
Palid pushed a commit that referenced this issue Oct 18, 2021
Palid pushed a commit that referenced this issue Oct 18, 2021
Palid pushed a commit that referenced this issue Oct 18, 2021
Palid pushed a commit that referenced this issue Oct 19, 2021
Palid pushed a commit that referenced this issue Oct 19, 2021
Palid pushed a commit that referenced this issue Oct 19, 2021
Palid pushed a commit that referenced this issue Oct 19, 2021
Developer experience automation moved this from Ready to develop to Done Feb 18, 2022
Web App Team automation moved this from In Review to In Test Feb 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Developer-Experience A-DevTools /devtools, show hidden events, etc. T-Enhancement
Projects
No open projects
Development

No branches or pull requests

3 participants