ArticlesBlog

Logging in PHP | PHP monolog tutorial | composer basic tutorial | composer packages

Logging in PHP | PHP monolog tutorial | composer basic tutorial | composer packages


alright guys so in the previous video we
have seen error logging well any logging is very important and but the problem is
if you log the errors and warnings this was a code which we have seen in
the previous video. if you did not watch the video I will put a link somewhere here
so you can watch that it’s nice to see that video I open Firefox by mistake yep
so we’ve seen how we can log errors in PHP but we should be logging more than
just errors for example suppose we want to track what the user is trying to do
like for some statistics or whatever like we need data and okay so what
happens this like suppose you want to track what the uses is trying to do – for
example they are in the logins screen then after that they’ve been to the
dashboard and then from there they’re going somewhere else so we should be
logging what a user is trying to do or something like that it will help us
maybe really give us some insight about what the user is trying to do – before leaving
yeah so we need to log log and so I will tell you how we can log how we can log
log okay how we can create a log the way we have to do that is a bit different
from the error logging code which is this yeah so okay one thing I forgot to
mention is there are three types of error errors which PHP generates
one is notice which is just notice I think and then there is warning and then
there is fatal error so yeah you can check them in the PHP documentation I
think I’m correct this notice warning for later okay
so for this section what we can do is we can use we can really we can remake the
wheel we can we can recreate the wheel and we can do something which we can
write a code which will help us log in a text file or any other file basically
the approach would be like to get a file to open the file to get all the contents
of the file and then our like more appropriately to append to the existing
files some text this is what rock means and the text will contain the date the
date and time and all these things and then some clock messages we can’t do
that but what you are to understand is like there’s no need for recruiting the
wheel every time PHP has an awesome community and we should take advantage
of what you already have well what I’m talking about is we can
use composure composure gives us if you don’t know what composure is probably
you should check it out composure not logging composure yes we’ll be using
that but for now composure so composure is like the npm of PHP is how I like to
think of it so composure and this a website get composure at all dependency
manager for PHP well composer gives us a lot of libraries and which we can use in
our code. this is open source and maintained by the community you can see
how many downloads are there like the how is usage what is the usage
and everything is there so we can use that instead of making our own thing and
this is probably what is he used in the actual production system if you
have checked code for example like some API calls
Google support a quite a lot of languages like JavaScript Python PHP if
you have checked that there is always a composer thing they have a composer.json
ad we have to install that so they use composer everyone uses composer in
production so we should also use composer
so the module or what should I say the package the package of composer which we
are going to be using is monologue now monologue I’ll show you in just a bit
yeah so this is a package list package insert all this literally called a
package list so if you know type your monologue you will get the first result
monologue monologue is just sit number install this is a number of installs so
monologue is a stable library and it’s pretty popular and you can see their
requirements so PHP 5.3 is good enough my the PHP in my server is less
than five point five yeah that’s 5.4 so it will work so there may be some will
these are things which we don’t really pay attention to moving and learning
some languages but the thing is there will be some web servers of your clients
or your web server maybe which has an old PHP installed like sometimes your
clients may not upgrade their PHP web servers for some reason and so okay it’s
sound yeah okay so that’s what I’m trying to say is you must see the PHP
version like we should be paying attention to these details when you’re
gay I’m getting more experience so okay the PHP 5.3 is good enough almost all the
web servers use speech 5.3 these days so this is one look you can see the
document here’s some github I’ll open the
documentation here actually so there’s no need for this one so this is
installation so it’s against the installation install latest version with
composer require monologue slash monologue so listen to copy this thing
and paste it in our command prompt if you’re using leanness it’s really better
a particular so the first thing is you should install composer usually if you
not know if you can be a composer not installed do that first and then do this
okay this one more time this we’re gonna check yeah okay so it’s gonna take some
time probably so let’s have this awkward silence okay singing awkward yeah it
takes some time I know but it’ll work okay the one thing I should be saying
while you’re waiting is them there will be two files a generated compositor
Jason and composited log got the advantage of using composer is suppose
there is a update in your for this call in the package suppose the average
monologue so when you are giving a software to your client in a given
software to your client when I extra to a client instead of you may have to
manually update the package yourself but you tell your client is to run composite
there and it will automatically automatically get their latest version
so this really advantageous waichman rows actually used so we’re going to
open the folder and as you can see they’re three files to create it
compositor case and composite lock and bender fan
has monologue here and there’s this file called autoload dot PHP so if we include
this file or another PC I should say require which if you because I am I
believe in require more than in air include so if you review to require this
file or another PHP to use the monologue library and I’m gonna show you this in a
moment here so yeah so let’s open this compositor Jason here and see what is
written here it says require monologue / monologue and greater than version 1.2 3
so yeah this will do so now if I delete this folder here of monologue and if I
run the composer you had a composer comma and I don’t really know but
composer install I think yeah you it’s most robably composer install and the
command is there in the website so you don’t really need to memorize it or just
with Church documentation may be so wait this is the installer download no no no
not this one yeah check the documentation yeah basic usage just once
set okay so if you do PHP not gonna repeat composer install should do the
thing actually okay okay the thing is I will show this in a later video maybe I
can’t find at this moment into something composer install I think this is a
command I did not check it actually yeah so I didn’t we installed a bridge
anything or code files so if I am courageous and have to delete this if I
delete this and if I run this again then yeah so the thing is if we delete this I
mean if you run this composer in story to automatically get the package from
the internet and it will save us out of headaches so yeah one log is shaking so
yeah I was right it is composer installed okay okay so
now let’s get back just go to the documentation of monologue and see so
the basic usage is PHP menu to use this this class I think yeah it’s a class and
this is object new log of name okay one thing which they have not said here is I
think they should have said we should have a first call out loader PHP so but
I think they think that it is probably very trivial so did not see it maybe so
okay first let’s make the now let’s make the log file so save it log dot txt and
now here that’s to thing like what I said is was going to require that file
or load or PHP so we should do that the first thing if we don’t do that we
cannot use the monologue packages so require and in the previous video which
you have not watched you should watch because there I explained the importance
of relative part and there are a few errors if we use just path like dot dot
slash and whatever so there’s a problem with that and we should use this method
I explained this in the previous video so you should probably watch that dot
okay no yeah this is the same folder so no problem
so it’s locked or txt so no no no burl doing what am i doing okay
so require their name file and then what we have to require is actually autoload
dot PHP so vendor slash autoload dot PHP and it is here or a little PHP so
display error lacera – one here because otherwise we cannot see the errors
ourself and let’s remove this line because we didn’t want to generate
errors at this moment okay okay so we require this file not
include require because I believe may require more than include and then let’s
print some line here just for the sake of it so that we understand that our
thing is getting is rooted so h1 and in text dot PHP so let’s see ok so yeah the
require is getting to all executed and now uh now let’s copy-paste this thing
because we reusing it okay so if we paste this here and do
some formatting and then okay I explain this thing actually so use Morelos this
sloka this there’s a class called mono look and there maybe there is a class
called lager and then these are all classes I think I think and after we
include these classes we are creating object called log with when you whenever
use the new keyword we means we’re in essentially an object and this is the
parameter this will be the name we’ll see that when you see that actual demo
okay so we are creating an object here and then oh this this is a method of Chi
unlock push handler new stream hand up path to your log files so path to my log
file is slash log dot txt and but first way to do this one again this is
important so there’s a lot of problem here they face because of this and I I
just have to keep stressing on this why it’s important okay so let’s see if
there are any errors here there should not be there is none as expected
okay now after recreating this object we can use this object to actually log
something and the method is is written here add records to your log okay this
is creating a log Channel but this is actually class and object they are
calling it channel what is actually has an object so we’re using this object to
now fire these methods routered warning how could error log non this one taught
this object so yeah okay so let’s run this and see what
happens oh my god what do you see
daytime before you don’t say the name – sometimes only quiet please okay okay
okay okay so if you see the errors every single edit is about that date time so I
think I should probably say that at 10:00 right now which I did not do in
the previous video so this is a log dot txt and this is the errorlog dot txt in
the error log as you can see here this is 8 Jan yesterday you know this this
you can see here like everything is engineer the warning add record in the
add record thing and record everything in the add record function did their
default time get so this is the thing this is getting then is written here in
the law of text let’s let’s see this thing this is the date this is a time
this is we should probably set the time as we need it right now
this is name where from where this name coming the name is coming from here if
we created a locker with name in AME instead of this week if you write our
name code works it’s a religion with code works so name then warning why
warning warning because first whatever we call was warning second one was error
so first warning then error then the text then there’s two arrays which them
do which we do not really mean at this moment so if you don’t need it no point
in doing that okay so there one more thing which
can do here is instead of error we can write info so info bar bar okay
there’s a joke here so if you if you know what I’m trying to say have a laugh
otherwise so okay first you to set that time so on so it’s really getting a pain
in the mmm so set underscore timezone this is a command I guess or not
nope so let’s check it PHP is set time zone so there’s a manual here P you can
use the PHP manual was a command date default timezone set okay this was a
comma this was the command okay so dead
default timezone set and the parameter is a sheer slash Kolkata I hope that I
get this right this time yes I got you right so I got it right I am usually
very bad at remembering commands because we have this awesome PHP documentation
and I don’t really find any why she should be remembering commands
that’s this wasn’t what I don’t really believe in so um yeah so I don’t really
remember commands but yeah this is my remember so as far as been changed of
course we have run the file again we have exited the three locking commands
and here apparently influenced not here now what is that what is that okay so if
we jump to this page log debug if he is copy if he just one
second dog don’t Oh quickie come on push handler push under we have done logger debug because I was in your locker debug
and this is actually this is thing like this means like we cannot create object
of this this is a constant I don’t know why I’m talking about class and object
here some tutorial of class object in PHP so the or push and when to use this
first to copy this line okay what I’m trying to do is I’m this info is
supported so let’s see why this is not working at this moment okay
let’s refresh the page and let’s see variable undefined variable logo okay of
course it’s log not logo here oh gee log and yeah so this is the thing it was
because of logger debug there are apparently few things here it’s debug
detail debug information info interesting events in something user
roles SQL laws notice over these are the monologue supported logging levels a dot
PHP locking level so let’s see PHP logging levels I don’t think it really did here
actually not this one I’m trying to tell three actually in PHP what’s this one if
I if it’s not there there is gonna skip it now mmm
it’s not here this is not remember what I’m trying to find actually I’ve seen
that the last time isn’t this is not that fun okay read that out because I
left that page opened in my mobile phone and also read it out I’m just going to
put the link to this website maybe it is from code minded at i/o blog PHP
interview questions sample answer so hitting this thing is a day and see the
suit in here actually oops so I’m going to open this page here
again and I will tell you exactly what the three blogging levels are so this is
a problem actually we have to put logger deeper which he did not do that this is
not in the first documentation and then like we can use warning error and info
which is better actually see warning error and info so if is speech again I
can see three more so we can basically put anything in the in this like we can
put anything and it will get locked so it’s a very nice thing actually so what
if just get some risk a really bold here and what if we do something like server
let’s lock the entire server all right you look at some error maybe let’s see
if this yeah I had two string conversion of course yeah this is what I’m thinking
editing visionary so let’s do printer and nowadays refresh yeah oh we’re not
getting that okay so this is interesting so what we can do is we can do something
like implode and then the first parameter of implode is hmm new line the
second parameter is array refresh this and yeah so this is it so what the
implode function does is is express an array with a delimiter then it’s pretty
limited like it combines an area with a delimiter here not without the
denominator here is /n you can do anything here anyway put a space but it
will probably it’s probably better to put /n because there’s a new line and
yeah so you block anything actually see if array like use implode important
explode are really important really useful functions in PHP and I use them a
lot so you can see you can lock anything and everything it’s probably best not to
log server message like in that server because yeah because the logs will get
really really huge and they something known as noisy logging which you should
not do actually longing means logging something but then it has a lot of noise
from the actual we should be locking so you should avoid that
no noise logins so this is all for this video
okay as I promised the three log in levels of PHP I’ve opened the page here
let’s find this question real quick okay this is the in PHP there are three main
type of errors notice warning fatal so notice means simple non critical
errors that are occurred during the script execution an example of a notice
would be accessing an undefined variable warning more important errors and notice
however the script continued to is securi continue execution an example
would be include a file that is not exist fatal this type of error causes a
termination of the script execution which occurs an example of fatal error
would be accessing a property of a non-existent object or require a
non-existent file so air types urn the author says understanding and the error
types is very important as they help developers understand what is going on
during the development and what to look for look out for during debugging so
yeah this is actually true and to look at what is happening at debugging you
should log there as so to log there is it is in the previous video they said
about three or four times in this video this is error log which will block
everything and then toward the our lock this is the log file so this is all for
this video if you like it gives a to give it a thumbs up and see you guys
next time and yeah to learn composers it’s really useful so yeah
I’ll go right now alcohol bye

Comment here