-
Notifications
You must be signed in to change notification settings - Fork 135
Created a log file for all commands sent by users #713 #717
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
Merged
+100
−8
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1aa6996
Created a log file for all commands sent by users #713
Luquitasjeffrey 7a2ad46
Merge remote-tracking branch 'upstream/main' into Issue713
Luquitasjeffrey 18ea3b8
Fixing typescript errors and making the log just text wothout termina…
Luquitasjeffrey 442c6ac
Documenting the configuration of command logging
Luquitasjeffrey 1571323
Fixing esling errors
Luquitasjeffrey 0214f67
Removed unit test that checks the count of middlewares registered on …
Luquitasjeffrey 3a335ad
Code formatting and added a configuration variable of the max size of…
Luquitasjeffrey e6ddb33
Avoid showing complete order message when can only show order ID in c…
Luquitasjeffrey f095730
Code formatting #713
Luquitasjeffrey a8df966
Fix typo in maxsize calculation #713
Luquitasjeffrey 0a73271
Changed unit of size limit of log files from GB to MB, and added MAX_…
Luquitasjeffrey a53b074
Code formatting
Luquitasjeffrey 6423e67
Use property shorthand for "maxFiles"
Luquitasjeffrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ node_modules | |
| admin.macaroon | ||
| tls.cert | ||
| dist/ | ||
| .history/ | ||
| .history/ | ||
| commands.log | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { MiddlewareFn } from 'telegraf'; | ||
| import { CommunityContext } from '../modules/community/communityContext'; | ||
| import winston from 'winston'; | ||
|
|
||
| const logFile = process.env.COMMAND_LOG_FILE || 'commands.log'; | ||
|
|
||
| const logger = winston.createLogger({ | ||
| format: winston.format.combine( | ||
| winston.format.timestamp({ | ||
| format: 'YYYY-MM-DDTHH:mm:ss.SSSZ', | ||
| }), | ||
| winston.format.printf(info => { | ||
| return `[${info.timestamp}] ${info.level}: ${info.message} ${ | ||
| info.stack ? info.stack : '' | ||
| }`; | ||
| }), | ||
| ), | ||
| levels: winston.config.syslog.levels, | ||
| level: 'debug', | ||
| transports: [ | ||
| new winston.transports.File({ | ||
| filename: logFile, | ||
| maxsize: 5 * 1024 * 1024 * 1000, // 5GB | ||
| }), | ||
| ], | ||
| exitOnError: false, | ||
| }); | ||
|
|
||
| export function commandLogger(): MiddlewareFn<CommunityContext> { | ||
| return async (ctx, next) => { | ||
| try { | ||
| if (ctx.message && 'text' in ctx.message) { | ||
| const msg = ctx.message; | ||
| const text = msg.text.trim(); | ||
| const userId = msg.from?.id ?? 'unknown'; | ||
|
|
||
| let command: string | null = null; | ||
| let args: string[] = []; | ||
| let isCommand: boolean; | ||
|
|
||
| if (text.startsWith('/')) { | ||
| const parts = text.split(/\s+/); | ||
| command = parts[0]; | ||
| args = parts.slice(1); | ||
| isCommand = true; | ||
| } else { | ||
| isCommand = false; | ||
| command = text; | ||
| } | ||
|
|
||
| const userName = msg.from?.username ?? ''; | ||
|
|
||
| logger.info(`User @${userName} [${userId}] ${isCommand? 'executed command:' : 'sent message:'} ${command} with args: [${args.join(', ')}]`); | ||
| } else if (ctx.callbackQuery && 'data' in ctx.callbackQuery) { | ||
| let msgText: string; | ||
| // Safely attempt to get message text | ||
| msgText = (ctx.callbackQuery?.message as any)?.text ?? ''; | ||
| const callbackData = ctx.callbackQuery.data; | ||
| const userName = ctx.callbackQuery.from?.username ?? ''; | ||
| const userId = ctx.callbackQuery.from?.id ?? ''; | ||
| logger.info(`User @${userName} [${userId}] sent callback query with data: ${callbackData}. Message text: '${msgText}'`); | ||
Catrya marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| logger.info(`Received non-command message or update from user.`); | ||
| } | ||
| } catch (err) { | ||
| logger.error('logging middleware failed', err); | ||
| } | ||
|
|
||
| return next(); | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.