Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
Implementing application logging and need assistance. My idea is to use an <cms:embed> with variables to writeout snippet to handle writing to a log file. This way I can set the log filenames depending on the part of the application with the same <cms:embed> throughout the application.

Each part of the application has a different configuration file embed and inside their are two <cms:set> variables for whether the logging should occur and the filename to use.

Code: Select all
<cms:set log_status='1' scope='global' />
<cms:set log_file='.charges.log' scope='global' />


Inside the application there is <cms:php> block, my question is how can I get the php variable $total into the embed msg field within this <cms:php>, is this possible?

Here is a very basic version for example purposes:

Code: Select all
<cms:embed 'configuration.html' />

<cms:php>
    $total = "100.00";
    <cms:embed 'writeout.html' msg="- total {$total}" run=log_status file=log_file />
</cms:php>


Here is the writeout.html file:

Code: Select all
<cms:set go_log = "<cms:get 'run' local_only='1' />"  scope='local' />
<cms:if go_log="1">
    <cms:php>
        file_put_contents("<cms:get 'file' local_only='1' />", "<cms:get 'msg' local_only='1' />".PHP_EOL , FILE_APPEND | LOCK_EX);
    </cms:php>
</cms:if>


This currently works but the total does not pass into the log file, resulting in:

Code: Select all
- total 


Also open to ways to improve this logging performance wise if any, ty.
Blutbaden wrote: Implementing application logging and need assistance.
Also open to ways to improve this logging performance wise if any, ty.


Do not use code directly in page. Request it async from JS script with some timeout.

A code not relevant to generating HTML for the visitor will generate problems for the visitor due to its blocking nature.

If someone opens 10 product pages in tabs at once from bookmarks (or browser restarts with previously opened tabs) - what happens?

A blocking code will block page delivery and other pages will also wait in queue (often user's session file is blocked). Often such code has a provision to lock a file for exclusive writing (<cms:write>, <cms:log> and your example have exclusive lock set).

In our store example we can imagine that some code runs for half a second on top of normal page time. So 5th tab will need several seconds to open and 10th page will probably timeout.


Coincidentally, I started to work on a similar addon to exec some jobs without blocking the main page thread. :) I think to reuse in part KK's example from /addons/mosaic/gc/gc.php - it does some jobs in separate background process in AdminPanel for Couch's Mosaic addon. After toying with database queries and writing an addon for that (viewtopic.php?f=8&t=12757) my hands are free to try to cache some query requests. For caching (and many other stuff) I want a background worker to do jobs without slowing down visitors.
(Ideally, it could be a real independent separate process (as in ReactPHP, DriftPHP) but it is a really long shot and will not be available for Couch quickly because I have to learn a lot about it from scratch.)

A job will be started after visitor's browser receives HTML from server and page is delivered (tab stops loading). I think a sample addon is not a long task after I figure out how it behaves in response to 50 concurrent requests to a page. My example use case (besides caching queries) is this - a visitor sends a message from contact-us form and we attach a log of visited pages during his session. When we receive an email from a visitor we can quickly check what pages he visited before messaging us.

P.S. I test how things go with concurrent requests very simply with the help of Apache and console command -
Code: Select all
ab -n 500 -c 50 -l http://localhost/index.php
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
And by the way, you are not actually passing parameters to snippet, they are set globally. Use this model instead -
Code: Select all
<cms:embed 'mysnippet.html'
    param1='This string will appear inside the embedded snippet as param1'
    param2='Me too! (as param2)'>
</cms:embed> => must be a tag pair to be able to pass params to snippet


And don't miss these 2 very relevant topics
viewtopic.php?f=2&t=11379#p30090 (More info on scope='parent', scope='local', scope='global')
viewtopic.php?f=8&t=11368&start=10#p30174 (Reusable Functions in CouchCMS (tutorial))
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
Hi Trendoman thanks for the input and information.

I did abandon my attempts to do create this logging app and am using KK's <cms:write> to log when I need to as that did exactly what I needed with a small modification to his function to pass a run parameter so the logging can be turned on or off via the CMS admin.

This logging would be on a back-end logged in users only and each user would have their own unique file that their logging is occurring so in theory this should not lock out people from high activity or cause high cpu load. For example when I pass the file name I am also including the users k_page_id (30195_activity.log) and all the log files write to a specific directory.

I have thought about changing this to save to another clonable template (User Activity) and saving with db_persists instead, it would most likely be way more efficient than <cms:write>.

Again thanks for all your time you give us users on this form (same goes to KK) it may not be said enough but it is appreciated.
4 posts Page 1 of 1