hey everyone my name is mohit i am a developer advocate at salesforce welcome to another edition
of developer quick takes in this video i'm gonna show you how to stream logs
to your terminal using salesforce cli having logs on the terminal can easily help you in identifying
any errors that occur during the transaction without further due let's see that in action
in order to stream logs on your terminal you will need salesforce cli you can install salesforce cli
by visiting developer.salesforce.com slash tools slash sfdx cli i'm in my vs code i've
cloned a sample app easy spaces this app is part of the sample gallery app you can obtain this app
from the github of trailhead apps by visiting github.com trailhead apps and clicking on easy
spaces lwc the instructions for setting up a scratch arc with all the code and the config
are documented in the github readme for this project for tailing logs to the terminal
i recommend you use your operating system terminal open your terminal in a separate window for this
demonstration i have split my screen to have the terminal and the browser side by side
we cd into the project workspace this make sure that our terminal is connected to the right arg
to start tailing locks on the terminal type sfdx force apex log tail with a color parameter
the color parameter used above enables some basic color highlighting for method constructors
variable assignments as a first example let's say i want to watch the logs for apex
that's invoked from lightning components as a part of the lightning app page load
now as soon as i refresh this page you can see that all the logs have been streamed to my
terminal at this point i can copy this and paste it into another editor or you would want to filter
them as per your need let's take an example of how to filter for specific things from the log
so in this example let's say i want to watch only for circle statement execution so for
that i type the full command and then i use the unix grep command to say grep dash e dash e is
for expression and in the expression i type circle execute dash begin append circle execute dash end
this time i refresh my lightning page you can see that only soccer statements are logged so i can
look at all of my queries that's been executed as a part of the page load on similar lines
let's say that i'm interested in only seeing the variable assignments that happen during the page
load of the lightning component so i just change my grep expression to use variable assignment
now as you can see next time when i do a page load the logs only stream the variable
assignments another example where streaming logs to the terminal can be useful is when
you're debugging or running your test class in this example when i'm finished executing
our test class you can see that the logs stream to the terminal
you can also customize with your own colors for the logs by setting sfdx apex log color
map environment variable now this variable has to be pointed to a file path to a json file so
as you can see i have a simple json file called set lock color.json where i want the constructor
and the circle statements to be of different colors magenta and for soccer i want red color
this time when i tail the logs you can see that the logs that are streamed to my
terminal have the colors as indicated in the json file so i've read for the circle statements and
magenta for the constructors this makes it easy to find what you're looking for
you can explore more about the cli commands for tailing debug logs by using command help
note that you can also customize the debug log levels by using the debug
level parameter to create a custom debug level navigate to debug logs click on new
and say new debug log and choose the right level for the different categories provide a name
and save the debug level once you have your custom debug level you can run the apex lockdale command
with the debug level that you just saved finally i want to leave you with couple more useful commands
related to apex debugs logs first is apex log list command this lists all the logs along with their
ids and status you can use the log id to retrieve the details of log using apex log get command with
iparameter lastly if you need to retrieve multiple log files from last few executions you can execute
force apex log get with nflag and by specifying the directory where you want to retrieve using the
d parameter in this case i want to retrieve last five recent logs to my local log folder so as you
can see all the last file logs have been retrieved to my local workspace i hope this video gave you a
good overview of how to tail and retrieve logs to your local computer i hope this is going to make
your debugging and log analysis easier thanks for watching if you learned something new hit like and
if you want more content like this pushed directly to you click subscribe to receive notifications