New in Laravel 11: Context for Logging Global Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today I want to demonstrate to you another new feature of larl 11 which was released actually after the release of laravel 11 in a tweet by Taylor artwell so it is available in laravel 11.0 do something in my case it was 11.08 which is context and it was celebrated by Community quite heavily with more than 1,000 likes on that tweet and this is the short version and I will show you a bit longer more practical version in a minute so basically whenever in your application so you add a value to some key and that is saved then in the context for the rest of the application request life cycle including logs and Q jobs that may be a massive help for debugging so you would know which user for example or which other ID corresponds to some Q job or other operation that feature is officially documented so there is cont text documentation page and I will link that in the description below but as it often happens the official release and the docs are not that much about practical implementation so where and in which case to use that and let me show you my example so for example if we compare the Tweet by Taylor within HTTP request where exactly you would add that code and that should happen probably before controllers and before Q jobs somewhere globally and my best guess is middleware so I've created a separate middleware with PHP artison make middleware Command and added this to middleware which would save the current user ID then I register that middleware to run all the time automatically with the new larel 11 syntax with middleware so I pend my middleware globally so it will run on every request and by the way side note middleware doesn't mean restricting only in Middle wor you can set something for the rest of the application some people taking think that middlewares are just about if statement and restricting something checking like permission or something like that not necessarily middleware it's just a file that will run early in the application life cycle to check something but not necessarily restrict in this case it adds a value and then in the controller for example we're doing something and we want to log some operation and if you want that log message to contain user ID or any other information earlier before context what you would do there of course a few ways but one of the ways you would do that directly in the message for example or it's possible to add a parameter to the message to be honest I don't remember the exact syntax but there is a possibility but what if you want to add that log in multiple places so you would add user ID every time this is where context is beneficial you just log whatever you want and now if you run that request this is API request this is some result it doesn't really matter and we go to ll. log it adds user ID null for a reason in my case user ID is null indeed because I'm not logged in but if we log in and we can fake it not sure if you're aware of that but there's o login user ID for example user ID one and now if we launch that in the postman nothing changes in the response but in the laral log you see user ID one so this is how context Works anywhere in any controller you just log something and that contains whatever information you saved in the context but if we go back to the original tweet by Taylor what he emphasized is that contextual data is also retained by any cued jobs what does that mean actually for example if you dispatch some job from the controller for example like this and you want that job to contain some information like user ID then you probably need to pass that ID as part of the job so for example inside of that job I've created this log info and now if we launch that send in lar of a log we have getting users but we don't have job getting users yet because we haven't launched the queue the Q worker hasn't been started yet so that job may be executed in an hour for example where the session of user ID is long over but the context would still appear so now if we launch Q work we have one job running and now if we go to Lal log we have that including user ID one so this is kind of an oversimplified maybe silly example with user ID but you should get the whole idea of life cycle when to set the context and how to use it and what is the benefit of course in case of user ID you may use o ID directly but instead of user ID here you may have any value for example generate some uid for Unique trace for this specific request for login or theb purposes which may be not available in any session or or variable and after the context feature was released an interesting example appeared on Twitter by David co-creator of laravel Nova who used context as a multi-tenancy feature so adding current tenant and then using that elsewhere but then despite 200 likes under that tweet there were a few comments against that usage of context first Michael pointed out that there's EST user and there's also off user globally also John pointed out that config could achieve maybe the same thing because you can set Global values in the config as well so I'm not sure about that particular approach of multi tency it's debatable I would prefer to use certain packages for multi tency because they were built specifically for that for context feature I see the main use case is quick logging of certain quick variables but what do you think what use cases do you in vision for such feature of context let's discuss in the comments below and if you want to receive more news about laral 11 new features new tools and other related stuff subscribe to the channel I will keep shooting daily videos that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 6,188
Rating: undefined out of 5
Keywords:
Id: vcSB91u2wdQ
Channel Id: undefined
Length: 6min 16sec (376 seconds)
Published: Thu Mar 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.